<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width">
<meta name="theme-color" content="#222"><meta name="generator" content="Hexo 7.3.0">

  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32.ico">
  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16.ico">
  <link rel="mask-icon" href="/images/logo.svg" color="#222">

<link rel="stylesheet" href="/css/main.css">



<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.5.1/css/all.min.css" integrity="sha256-wiz7ZSCn/btzhjKDQBms9Hx4sSeUYsDrTLg7roPstac=" crossorigin="anonymous">
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/animate.css/3.1.1/animate.min.css" integrity="sha256-PR7ttpcvz8qrF57fur/yAx1qXMFJeJFiA6pSzWi0OIE=" crossorigin="anonymous">
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/fancyapps-ui/5.0.28/fancybox/fancybox.css" integrity="sha256-6cQIC71/iBIYXFK+0RHAvwmjwWzkWd+r7v/BX3/vZDc=" crossorigin="anonymous">
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/pace/1.2.4/themes/green/pace-theme-minimal.css">
  <script src="https://cdnjs.cloudflare.com/ajax/libs/pace/1.2.4/pace.min.js" integrity="sha256-gqd7YTjg/BtfqWSwsJOvndl0Bxc8gFImLEkXQT8+qj0=" crossorigin="anonymous"></script>

<script class="next-config" data-name="main" type="application/json">{"hostname":"sumumm.github.io","root":"/","images":"/images","scheme":"Gemini","darkmode":false,"version":"8.19.2","exturl":false,"sidebar":{"position":"left","display":"post","padding":18,"offset":12},"copycode":{"enable":true,"style":"mac"},"fold":{"enable":true,"height":300},"bookmark":{"enable":false,"color":"#222","save":"auto"},"mediumzoom":false,"lazyload":true,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"stickytabs":false,"motion":{"enable":true,"async":true,"transition":{"menu_item":"fadeInDown","post_block":"fadeIn","post_header":"fadeInDown","post_body":"fadeInDown","coll_header":"fadeInLeft","sidebar":"fadeInUp"}},"i18n":{"placeholder":"搜索...","empty":"没有找到任何搜索结果：${query}","hits_time":"找到 ${hits} 个搜索结果（用时 ${time} 毫秒）","hits":"找到 ${hits} 个搜索结果"},"path":"/search.xml","localsearch":{"enable":true,"trigger":"auto","top_n_per_article":1,"unescape":false,"preload":false}}</script><script src="/js/config.js"></script>

    <meta name="description" content="前面我们已经注册了自己的总线，并可以在总线上注册设备和驱动，那么两者怎么匹配？若笔记中有错误或者不合适的地方，欢迎批评指正😃。">
<meta property="og:type" content="article">
<meta property="og:title" content="LV06-04-linux设备模型-09-设备与驱动的匹配">
<meta property="og:url" content="https://sumumm.github.io/post/6e6a5ac3.html">
<meta property="og:site_name" content="苏木">
<meta property="og:description" content="前面我们已经注册了自己的总线，并可以在总线上注册设备和驱动，那么两者怎么匹配？若笔记中有错误或者不合适的地方，欢迎批评指正😃。">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/02IMX6ULL%E5%B9%B3%E5%8F%B0/LV06-%E9%A9%B1%E5%8A%A8%E5%BC%80%E5%8F%91/LV06-04-linux%E8%AE%BE%E5%A4%87%E6%A8%A1%E5%9E%8B-09-%E8%AE%BE%E5%A4%87%E4%B8%8E%E9%A9%B1%E5%8A%A8%E7%9A%84%E5%8C%B9%E9%85%8D/img/image-20250113155339512.png">
<meta property="og:image" content="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/02IMX6ULL%E5%B9%B3%E5%8F%B0/LV06-%E9%A9%B1%E5%8A%A8%E5%BC%80%E5%8F%91/LV06-04-linux%E8%AE%BE%E5%A4%87%E6%A8%A1%E5%9E%8B-09-%E8%AE%BE%E5%A4%87%E4%B8%8E%E9%A9%B1%E5%8A%A8%E7%9A%84%E5%8C%B9%E9%85%8D/img/image-20250113155426552.png">
<meta property="og:image" content="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/02IMX6ULL%E5%B9%B3%E5%8F%B0/LV06-%E9%A9%B1%E5%8A%A8%E5%BC%80%E5%8F%91/LV06-04-linux%E8%AE%BE%E5%A4%87%E6%A8%A1%E5%9E%8B-09-%E8%AE%BE%E5%A4%87%E4%B8%8E%E9%A9%B1%E5%8A%A8%E7%9A%84%E5%8C%B9%E9%85%8D/img/image-20250113155534905.png">
<meta property="og:image" content="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/02IMX6ULL%E5%B9%B3%E5%8F%B0/LV06-%E9%A9%B1%E5%8A%A8%E5%BC%80%E5%8F%91/LV06-04-linux%E8%AE%BE%E5%A4%87%E6%A8%A1%E5%9E%8B-09-%E8%AE%BE%E5%A4%87%E4%B8%8E%E9%A9%B1%E5%8A%A8%E7%9A%84%E5%8C%B9%E9%85%8D/img/image-20250113155704752.png">
<meta property="og:image" content="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/02IMX6ULL%E5%B9%B3%E5%8F%B0/LV06-%E9%A9%B1%E5%8A%A8%E5%BC%80%E5%8F%91/LV06-04-linux%E8%AE%BE%E5%A4%87%E6%A8%A1%E5%9E%8B-09-%E8%AE%BE%E5%A4%87%E4%B8%8E%E9%A9%B1%E5%8A%A8%E7%9A%84%E5%8C%B9%E9%85%8D/img/image-20250113155843741.png">
<meta property="og:image" content="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/02IMX6ULL%E5%B9%B3%E5%8F%B0/LV06-%E9%A9%B1%E5%8A%A8%E5%BC%80%E5%8F%91/LV06-04-linux%E8%AE%BE%E5%A4%87%E6%A8%A1%E5%9E%8B-09-%E8%AE%BE%E5%A4%87%E4%B8%8E%E9%A9%B1%E5%8A%A8%E7%9A%84%E5%8C%B9%E9%85%8D/img/image-20250113160006054.png">
<meta property="og:image" content="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/02IMX6ULL%E5%B9%B3%E5%8F%B0/LV06-%E9%A9%B1%E5%8A%A8%E5%BC%80%E5%8F%91/LV06-04-linux%E8%AE%BE%E5%A4%87%E6%A8%A1%E5%9E%8B-09-%E8%AE%BE%E5%A4%87%E4%B8%8E%E9%A9%B1%E5%8A%A8%E7%9A%84%E5%8C%B9%E9%85%8D/img/image-20250113193242318.png">
<meta property="og:image" content="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/02IMX6ULL%E5%B9%B3%E5%8F%B0/LV06-%E9%A9%B1%E5%8A%A8%E5%BC%80%E5%8F%91/LV06-04-linux%E8%AE%BE%E5%A4%87%E6%A8%A1%E5%9E%8B-09-%E8%AE%BE%E5%A4%87%E4%B8%8E%E9%A9%B1%E5%8A%A8%E7%9A%84%E5%8C%B9%E9%85%8D/img/image-20250115142202576.png">
<meta property="og:image" content="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/02IMX6ULL%E5%B9%B3%E5%8F%B0/LV06-%E9%A9%B1%E5%8A%A8%E5%BC%80%E5%8F%91/LV06-04-linux%E8%AE%BE%E5%A4%87%E6%A8%A1%E5%9E%8B-09-%E8%AE%BE%E5%A4%87%E4%B8%8E%E9%A9%B1%E5%8A%A8%E7%9A%84%E5%8C%B9%E9%85%8D/img/linux_device_modle000.png">
<meta property="og:image" content="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/02IMX6ULL%E5%B9%B3%E5%8F%B0/LV06-%E9%A9%B1%E5%8A%A8%E5%BC%80%E5%8F%91/LV06-04-linux%E8%AE%BE%E5%A4%87%E6%A8%A1%E5%9E%8B-09-%E8%AE%BE%E5%A4%87%E4%B8%8E%E9%A9%B1%E5%8A%A8%E7%9A%84%E5%8C%B9%E9%85%8D/img/linux_device_modle003.png">
<meta property="article:published_time" content="2025-01-16T14:26:36.000Z">
<meta property="article:modified_time" content="2025-06-13T16:25:57.052Z">
<meta property="article:author" content="苏木">
<meta property="article:tag" content="LV06-驱动开发">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/02IMX6ULL%E5%B9%B3%E5%8F%B0/LV06-%E9%A9%B1%E5%8A%A8%E5%BC%80%E5%8F%91/LV06-04-linux%E8%AE%BE%E5%A4%87%E6%A8%A1%E5%9E%8B-09-%E8%AE%BE%E5%A4%87%E4%B8%8E%E9%A9%B1%E5%8A%A8%E7%9A%84%E5%8C%B9%E9%85%8D/img/image-20250113155339512.png">


<link rel="canonical" href="https://sumumm.github.io/post/6e6a5ac3.html">



<script class="next-config" data-name="page" type="application/json">{"sidebar":"","isHome":false,"isPost":true,"lang":"zh-CN","comments":"","permalink":"https://sumumm.github.io/post/6e6a5ac3.html","path":"post/6e6a5ac3.html","title":"LV06-04-linux设备模型-09-设备与驱动的匹配"}</script>

<script class="next-config" data-name="calendar" type="application/json">""</script>
<title>LV06-04-linux设备模型-09-设备与驱动的匹配 | 苏木</title>
  








    <script src="/js/browser_tools_disable.js"></script>

  <noscript>
    <link rel="stylesheet" href="/css/noscript.css">
  </noscript>
<!-- hexo injector head_end start --><link rel="stylesheet" href="https://unpkg.com/hexo-next-tags-plus@latest/lib/tag_plus.css" media="defer" onload="this.media='all'"><!-- hexo injector head_end end --></head>

<body itemscope itemtype="http://schema.org/WebPage" class="use-motion">
  <div class="headband"></div>

  <main class="main">
    <div class="column">
      <header class="header" itemscope itemtype="http://schema.org/WPHeader"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏" role="button">
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/" class="brand" rel="start">
      <i class="logo-line"></i>
      <p class="site-title">苏木</p>
      <i class="logo-line"></i>
    </a>
      <p class="site-subtitle" itemprop="description">我的学习之路</p>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger" aria-label="搜索" role="button">
        <i class="fa fa-search fa-fw fa-lg"></i>
    </div>
  </div>
</div>



<nav class="site-nav">
  <ul class="main-menu menu"><li class="menu-item menu-item-home"><a href="/" rel="section"><i class="fa fa-home fa-fw"></i>苏木的家</a></li><li class="menu-item menu-item-categories"><a href="/categories/" rel="section"><i class="fa fa-th fa-fw"></i>分类页<span class="badge">42</span></a></li><li class="menu-item menu-item-archives"><a href="/archives/" rel="section"><i class="fa fa-archive fa-fw"></i>归档页<span class="badge">673</span></a></li><li class="menu-item menu-item-flink"><a href="/flink/" rel="section"><i class="fa fa-link fa-fw"></i>友人帐</a></li><li class="menu-item menu-item-about"><a href="/about/" rel="section"><i class="fa fa-user fa-fw"></i>关于我</a></li>
      <li class="menu-item menu-item-search">
        <a role="button" class="popup-trigger"><i class="fa fa-search fa-fw"></i>搜索
        </a>
      </li>
  </ul>
</nav>



  <div class="search-pop-overlay">
    <div class="popup search-popup"><div class="search-header">
  <span class="search-icon">
    <i class="fa fa-search"></i>
  </span>
  <div class="search-input-container">
    <input autocomplete="off" autocapitalize="off" maxlength="80"
           placeholder="搜索..." spellcheck="false"
           type="search" class="search-input">
  </div>
  <span class="popup-btn-close" role="button">
    <i class="fa fa-times-circle"></i>
  </span>
</div>
<div class="search-result-container no-result">
  <div class="search-result-icon">
    <i class="fa fa-spinner fa-pulse fa-5x"></i>
  </div>
</div>

    </div>
  </div>

</header>
        
  
  <aside class="sidebar">

    <div class="sidebar-inner sidebar-nav-active sidebar-toc-active">
      <ul class="sidebar-nav">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <div class="sidebar-panel-container">
        <!--noindex-->
        <div class="post-toc-wrap sidebar-panel">
            <div class="post-toc animated"><ol class="nav"><li class="nav-item nav-level-1"><a class="nav-link" href="#%E4%B8%80%E3%80%81%E8%AE%BE%E5%A4%87%E5%92%8C%E9%A9%B1%E5%8A%A8%E5%8C%B9%E9%85%8D"><span class="nav-text">一、设备和驱动匹配</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#1-%E6%80%BB%E7%BA%BF"><span class="nav-text">1. 总线</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#1-1-xxx-match"><span class="nav-text">1.1 xxx_match()</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#1-2-xxx-probe"><span class="nav-text">1.2 xxx_probe()</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#1-3-sbus-demo-c"><span class="nav-text">1.3 sbus_demo.c</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#2-%E8%AE%BE%E5%A4%87"><span class="nav-text">2. 设备</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#2-1-%E8%AE%BE%E5%A4%87%E5%90%8D%E7%A7%B0"><span class="nav-text">2.1 设备名称</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#2-2-xxx-release"><span class="nav-text">2.2 xxx_release()</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#2-3-sdevice-demo-c"><span class="nav-text">2.3 sdevice_demo.c</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#3-%E9%A9%B1%E5%8A%A8"><span class="nav-text">3. 驱动</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#3-1-%E9%A9%B1%E5%8A%A8%E5%90%8D%E7%A7%B0"><span class="nav-text">3.1 驱动名称</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#3-2-xxx-probe"><span class="nav-text">3.2 xxx_probe()</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#3-3-xxx-remove"><span class="nav-text">3.3 xxx_remove()</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#3-4-sdriver-demo-c"><span class="nav-text">3.4 sdriver_demo.c</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#4-%E5%BC%80%E5%8F%91%E6%9D%BF%E6%B5%8B%E8%AF%95"><span class="nav-text">4. 开发板测试</span></a></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#%E4%BA%8C%E3%80%81xxx-probe-%E5%87%BD%E6%95%B0%E6%89%A7%E8%A1%8C%E6%B5%81%E7%A8%8B"><span class="nav-text">二、xxx_probe()函数执行流程</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#1-driver-attach"><span class="nav-text">1. driver_attach()</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#1-1-bus-for-each-dev"><span class="nav-text">1.1  bus_for_each_dev()</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#1-2-driver-attach"><span class="nav-text">1.2 __driver_attach() </span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#1-2-1-driver-match-device"><span class="nav-text"> 1.2.1 driver_match_device()</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#1-2-2-driver-probe-device"><span class="nav-text">1.2.2 driver_probe_device()</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#1-3-%E6%80%BB%E7%BB%93"><span class="nav-text">1.3 总结</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#2-really-probe"><span class="nav-text">2. really_probe()</span></a></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#%E4%B8%89%E3%80%81%E9%A9%B1%E5%8A%A8%E5%92%8C%E8%AE%BE%E5%A4%87%E5%8A%A0%E8%BD%BD%E7%9A%84%E5%85%88%E5%90%8E%E9%A1%BA%E5%BA%8F"><span class="nav-text">三、驱动和设备加载的先后顺序</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#1-device-add"><span class="nav-text">1. device_add()</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#2-device-attach"><span class="nav-text">2. __device_attach()</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#3-device-bind-driver"><span class="nav-text">3. device_bind_driver()</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#4-%E6%80%BB%E7%BB%93"><span class="nav-text">4. 总结</span></a></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#%E5%9B%9B%E3%80%81%E6%80%BB%E7%BB%93"><span class="nav-text">四、总结</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#1-%E8%AE%BE%E5%A4%87%E5%92%8C%E9%A9%B1%E5%8A%A8%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84%E5%85%B3%E7%B3%BB"><span class="nav-text">1. 设备和驱动数据结构关系</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#2-%E6%B3%A8%E5%86%8C%E6%B5%81%E7%A8%8B"><span class="nav-text">2. 注册流程</span></a></li></ol></li></ol></div>
        </div>
        <!--/noindex-->

        <div class="site-overview-wrap sidebar-panel">
          <div class="site-author animated" itemprop="author" itemscope itemtype="http://schema.org/Person">
    <img class="site-author-image" itemprop="image" alt="苏木"
      src="/images/avatar.jpg">
  <p class="site-author-name" itemprop="name">苏木</p>
  <div class="site-description" itemprop="description">莫道桑榆晚，为霞尚满天</div>
</div>
<div class="site-state-wrap animated">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
        <a href="/archives/">
          <span class="site-state-item-count">673</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
          <a href="/categories/">
        <span class="site-state-item-count">42</span>
        <span class="site-state-item-name">分类</span></a>
      </div>
      <div class="site-state-item site-state-tags">
        <span class="site-state-item-count">43</span>
        <span class="site-state-item-name">标签</span>
      </div>
  </nav>
</div>
  <div class="links-of-author animated">
      <span class="links-of-author-item">
        <a href="https://github.com/sumumm" title="GitHub → https:&#x2F;&#x2F;github.com&#x2F;sumumm" rel="noopener me" target="_blank"><i class="fab fa-github fa-fw"></i>GitHub</a>
      </span>
  </div>

        </div>
      </div>
    </div>

    
  </aside>


    </div>

    <div class="main-inner post posts-expand">


  


<div class="post-block">
  
  

  <article itemscope itemtype="http://schema.org/Article" class="post-content" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="https://sumumm.github.io/post/6e6a5ac3.html">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.jpg">
      <meta itemprop="name" content="苏木">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="苏木">
      <meta itemprop="description" content="莫道桑榆晚，为霞尚满天">
    </span>

    <span hidden itemprop="post" itemscope itemtype="http://schema.org/CreativeWork">
      <meta itemprop="name" content="LV06-04-linux设备模型-09-设备与驱动的匹配 | 苏木">
      <meta itemprop="description" content="">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          LV06-04-linux设备模型-09-设备与驱动的匹配
        </h1>

        <div class="post-meta-container">
          <div class="post-meta">
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar"></i>
      </span>
      <span class="post-meta-item-text">发表于</span>

      <time title="创建时间：2025-01-16 22:26:36" itemprop="dateCreated datePublished" datetime="2025-01-16T22:26:36+08:00">2025-01-16</time>
    </span>
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-folder"></i>
      </span>
      <span class="post-meta-item-text">分类于</span>
        <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
          <a href="/categories/%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/" itemprop="url" rel="index"><span itemprop="name">嵌入式开发</span></a>
        </span>
          ，
        <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
          <a href="/categories/%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/02IMX6ULL%E5%B9%B3%E5%8F%B0/" itemprop="url" rel="index"><span itemprop="name">02IMX6ULL平台</span></a>
        </span>
          ，
        <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
          <a href="/categories/%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/02IMX6ULL%E5%B9%B3%E5%8F%B0/LV06-%E9%A9%B1%E5%8A%A8%E5%BC%80%E5%8F%91/" itemprop="url" rel="index"><span itemprop="name">LV06-驱动开发</span></a>
        </span>
    </span>

  
    <span class="post-meta-break"></span>
    <span class="post-meta-item" title="本文字数">
      <span class="post-meta-item-icon">
        <i class="far fa-file-word"></i>
      </span>
      <span class="post-meta-item-text">本文字数：</span>
      <span>11k</span>
    </span>
    <span class="post-meta-item" title="阅读时长">
      <span class="post-meta-item-icon">
        <i class="far fa-clock"></i>
      </span>
      <span class="post-meta-item-text">阅读时长 &asymp;</span>
      <span>40 分钟</span>
    </span>
</div>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody"><p>前面我们已经注册了自己的总线，并可以在总线上注册设备和驱动，那么两者怎么匹配？若笔记中有错误或者不合适的地方，欢迎批评指正😃。</p>
<span id="more"></span>

<!-- Photo: https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/02IMX6ULL%E5%B9%B3%E5%8F%B0/LV06-%E9%A9%B1%E5%8A%A8%E5%BC%80%E5%8F%91/LV06-04-linux%E8%AE%BE%E5%A4%87%E6%A8%A1%E5%9E%8B-09-%E8%AE%BE%E5%A4%87%E4%B8%8E%E9%A9%B1%E5%8A%A8%E7%9A%84%E5%8C%B9%E9%85%8D/img/ -->

<details class="folding-tag" blue><summary> 点击查看使用工具及版本 </summary>
              <div class='content'>
              <table>    <tr>        <td align="center" rowspan="5">PC端开发环境</td>        <td align="center" width=150px>Windows</td>        <td align="left">Windows11</td>    </tr>    <tr>        <td align="center">Ubuntu</td>        <td align="left">Ubuntu20.04.2的64位版本</td>      </tr>    <tr>        <td align="center">VMware® Workstation 17 Pro</td>        <td align="left">17.6.0 build-24238078</td>      </tr>    <tr>        <td align="center">终端软件</td>        <td align="left">MobaXterm(Professional Edition v23.0 Build 5042 (license))</td>    </tr>    <tr>        <td align="center">Win32DiskImager</td>        <td align="left">Win32DiskImager v1.0</td>      </tr>    <tr>        <td align="center" rowspan="3">Linux开发板环境</td>        <td align="center">Linux开发板</td>        <td align="left">正点原子 i.MX6ULL Linux 阿尔法开发板</td>      </tr>    <tr>        <td align="center">uboot</td>        <td align="left">NXP官方提供的uboot，使用的uboot版本为U-Boot 2019.04</td>      </tr>    <tr>        <td align="center">linux内核</td>        <td align="left">linux-4.19.71(NXP官方提供)</td>      </tr></table>
              </div>
            </details>

<details class="folding-tag" blue><summary> 点击查看本文参考资料 </summary>
              <div class='content'>
              <table>    <tr>        <td align="center">分类</td>        <td align="center">网址</td>        <td align="center">说明</td>    </tr>    <tr>        <td align="center" rowspan="5">官方网站</td>        <td align="left"><a href="https://www.arm.com/" target="_blank">https://www.arm.com/</a></td>        <td align="left">ARM官方网站，在这里我们可以找到Cotex-Mx以及ARMVx的一些文档</td>    </tr>    <tr>        <td align="left"><a href="https://www.nxp.com.cn/" target="_blank">https://www.nxp.com.cn/ </a></td>        <td align="left">NXP官方网站</td>    </tr>    <tr>        <td align="left"><a href="https://www.nxpic.org.cn/" target="_blank">https://www.nxpic.org.cn/</a></td><td align="left">NXP 官方社区</td>    </tr>    <tr>        <td align="left"><a href="https://u-boot.readthedocs.io/en/latest/" target="_blank">https://u-boot.readthedocs.io/en/latest/</a></td><td align="left">u-boot官网</td>    </tr>    <tr>        <td align="left"><a href="https://www.kernel.org/" target="_blank">https://www.kernel.org/</a></td><td align="left">linux内核官网</td>    </tr></table>
              </div>
            </details>

<details class="folding-tag" blue><summary> 点击查看相关文件下载 </summary>
              <div class='content'>
              <table>    <tr>        <td align="center">分类</td>        <td align="center">网址</td>        <td align="center">说明</td>    </tr>    <tr>        <td align="center" rowspan="3">NXP</td>        <td align="left"><a href="https://github.com/nxp-imx" target="_blank">https://github.com/nxp-imx</a></td>        <td align="left">NXP imx开发资源GitHub组织，里边会有u-boot和linux内核的仓库</td>    </tr>    <tr>        <td align="left"><a href="https://github.com/nxp-imx/linux-imx/releases/tag/v4.19.71" target="_blank">nxp-imx/linux-imx/releases/tag/v4.19.71</a></td>        <td align="left">NXP linux内核仓库tags中的v4.19.71</td>    </tr>    <tr>        <td align="left"><a href="https://github.com/nxp-imx/uboot-imx/releases/tag/rel_imx_4.19.35_1.1.0" target="_blank">nxp-imx/uboot-imx/releases/tag/rel_imx_4.19.35_1.1.0</a></td>        <td align="left">NXP u-boot仓库tags中的rel_imx_4.19.35_1.1.0</td>    </tr>    <tr>        <td align="center" rowspan="2">I.MX6ULL</td>        <td align="left"><a href="https://www.nxp.com.cn/docs/en/data-sheet/IMX6ULLIEC.pdf" target="_blank">i.MX 6ULL Applications Processors for Industrial Products</a></td>        <td align="left">I.MX6ULL 芯片手册（datasheet，可以在线查看）</td>    </tr>    <tr>        <td align="left"><a href="https://www.nxp.com.cn/webapp/Download?colCode=IMX6ULLRM&lang_cd=zh" target="_blank">i.MX 6ULL Applications ProcessorReference Manual</a></td>        <td align="left">I.MX6ULL 参考手册（下载后才能查看，需要登录NXP官网）</td>    </tr>    <tr>        <td align="center" rowspan="3">Source Code</td>        <td align="left"><a href="https://elixir.bootlin.com/linux/latest/source" target="_blank">https://elixir.bootlin.com/linux/latest/source</a></td>        <td align="left">linux kernel源码</td>    </tr>    <tr>        <td align="left"><a href="https://git.kernel.org/pub/scm/linux/kernel/git/stable/linux.git/tree/?h=v4.19.71&id=e7d2672c66e4d3675570369bf20856296da312c4" target="_blank">kernel/git/stable/linux.git - Linux kernel stable tree</a></td>        <td align="left">linux kernel源码(官网,tag 4.19.71)</td>    </tr>    <tr>        <td align="left"><a href="https://elixir.bootlin.com/u-boot/latest/source" target="_blank">https://elixir.bootlin.com/u-boot/latest/source</a></td>        <td align="left">uboot源码</td>    </tr></table>
              </div>
            </details>

<h1 id="一、设备和驱动匹配"><a href="#一、设备和驱动匹配" class="headerlink" title="一、设备和驱动匹配"></a><font size=3>一、设备和驱动匹配</font></h1><h2 id="1-总线"><a href="#1-总线" class="headerlink" title="1. 总线"></a><font size=3>1. 总线</font></h2><h3 id="1-1-xxx-match"><a href="#1-1-xxx-match" class="headerlink" title="1.1 xxx_match()"></a><font size=3>1.1 xxx_match()</font></h3><p>总线中，我们需要实现xxx_match()函数：</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="type">static</span> <span class="type">int</span> <span class="title function_">sbus_match</span><span class="params">(<span class="keyword">struct</span> device *dev, <span class="keyword">struct</span> device_driver *drv)</span></span><br><span class="line">&#123;</span><br><span class="line">    <span class="type">const</span> <span class="type">char</span> *device_name = dev_name(dev);</span><br><span class="line">    <span class="type">int</span> len = <span class="built_in">strlen</span>(drv-&gt;name);</span><br><span class="line"></span><br><span class="line">	PRT(<span class="string">&quot;dev name is %s, drv name is %s, name len = %d\n&quot;</span>, device_name, drv-&gt;name, len);</span><br><span class="line"></span><br><span class="line">	<span class="keyword">if</span> (!<span class="built_in">strncmp</span>(device_name, drv-&gt;name, len)) </span><br><span class="line">    &#123;</span><br><span class="line">		PRT(<span class="string">&quot;dev is %s &lt;---&gt; drv is %s, match success!\n&quot;</span>, drv-&gt;name, device_name);</span><br><span class="line">		<span class="keyword">return</span> <span class="number">1</span>;</span><br><span class="line">	&#125;</span><br><span class="line">    <span class="comment">//else的话就是没有匹配上</span></span><br><span class="line">	<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>我们在这里比较两个驱动的名称和设备的名称，若是一样，就表示匹配成功，需要返回1，若是不同，就是没有匹配上，就返回0。那能反过来吗？比如匹配成功返回0，匹配失败返回1？这样不行，应该是调用的时候判断条件进行了限制。</p>
<h3 id="1-2-xxx-probe"><a href="#1-2-xxx-probe" class="headerlink" title="1.2 xxx_probe()"></a><font size=3>1.2 xxx_probe()</font></h3><p>在总线中还需要实现一个xxx_probe()函数：</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * @brief  sbus_probe()</span></span><br><span class="line"><span class="comment"> * @note   设备探测的回调函数</span></span><br><span class="line"><span class="comment"> * @param [in]</span></span><br><span class="line"><span class="comment"> * @param [out]</span></span><br><span class="line"><span class="comment"> * @retval </span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="type">static</span> <span class="type">int</span> <span class="title function_">sbus_probe</span><span class="params">(<span class="keyword">struct</span> device *dev)</span></span><br><span class="line">&#123;</span><br><span class="line">    <span class="class"><span class="keyword">struct</span> <span class="title">device_driver</span> *<span class="title">drv</span> =</span> dev-&gt;driver;</span><br><span class="line">    <span class="type">const</span> <span class="type">char</span> *device_name = dev_name(dev);</span><br><span class="line"></span><br><span class="line">    PRT(<span class="string">&quot;device_name=%s driver_name=%s\n&quot;</span>, device_name, drv-&gt;name);</span><br><span class="line">    <span class="keyword">if</span> (drv-&gt;probe)</span><br><span class="line">    &#123;</span><br><span class="line">        drv-&gt;probe(dev); <span class="comment">// 调用驱动的 sdrv_probe() 探测函数</span></span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h3 id="1-3-sbus-demo-c"><a href="#1-3-sbus-demo-c" class="headerlink" title="1.3 sbus_demo.c"></a><font size=3>1.3 sbus_demo.c</font></h3><p>完整源码如下所示：</p>
<details class="folding-tag" blue><summary> 点击查看详情 </summary>
              <div class='content'>
              <figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span class="line">126</span><br><span class="line">127</span><br><span class="line">128</span><br><span class="line">129</span><br><span class="line">130</span><br><span class="line">131</span><br><span class="line">132</span><br><span class="line">133</span><br><span class="line">134</span><br><span class="line">135</span><br><span class="line">136</span><br><span class="line">137</span><br><span class="line">138</span><br><span class="line">139</span><br><span class="line">140</span><br><span class="line">141</span><br><span class="line">142</span><br><span class="line">143</span><br><span class="line">144</span><br><span class="line">145</span><br><span class="line">146</span><br><span class="line">147</span><br><span class="line">148</span><br><span class="line">149</span><br><span class="line">150</span><br><span class="line">151</span><br><span class="line">152</span><br><span class="line">153</span><br><span class="line">154</span><br><span class="line">155</span><br><span class="line">156</span><br><span class="line">157</span><br><span class="line">158</span><br><span class="line">159</span><br><span class="line">160</span><br><span class="line">161</span><br><span class="line">162</span><br><span class="line">163</span><br><span class="line">164</span><br><span class="line">165</span><br><span class="line">166</span><br><span class="line">167</span><br><span class="line">168</span><br><span class="line">169</span><br><span class="line">170</span><br><span class="line">171</span><br><span class="line">172</span><br><span class="line">173</span><br><span class="line">174</span><br><span class="line">175</span><br><span class="line">176</span><br><span class="line">177</span><br><span class="line">178</span><br><span class="line">179</span><br><span class="line">180</span><br><span class="line">181</span><br><span class="line">182</span><br><span class="line">183</span><br><span class="line">184</span><br><span class="line">185</span><br><span class="line">186</span><br><span class="line">187</span><br><span class="line">188</span><br><span class="line">189</span><br><span class="line">190</span><br><span class="line">191</span><br><span class="line">192</span><br><span class="line">193</span><br><span class="line">194</span><br><span class="line">195</span><br><span class="line">196</span><br><span class="line">197</span><br><span class="line">198</span><br><span class="line">199</span><br><span class="line">200</span><br><span class="line">201</span><br><span class="line">202</span><br><span class="line">203</span><br><span class="line">204</span><br><span class="line">205</span><br><span class="line">206</span><br><span class="line">207</span><br><span class="line">208</span><br><span class="line">209</span><br><span class="line">210</span><br><span class="line">211</span><br><span class="line">212</span><br><span class="line">213</span><br><span class="line">214</span><br><span class="line">215</span><br><span class="line">216</span><br><span class="line">217</span><br><span class="line">218</span><br><span class="line">219</span><br><span class="line">220</span><br><span class="line">221</span><br><span class="line">222</span><br><span class="line">223</span><br><span class="line">224</span><br><span class="line">225</span><br><span class="line">226</span><br><span class="line">227</span><br><span class="line">228</span><br><span class="line">229</span><br><span class="line">230</span><br><span class="line">231</span><br><span class="line">232</span><br><span class="line">233</span><br><span class="line">234</span><br><span class="line">235</span><br><span class="line">236</span><br><span class="line">237</span><br><span class="line">238</span><br><span class="line">239</span><br><span class="line">240</span><br><span class="line">241</span><br><span class="line">242</span><br><span class="line">243</span><br><span class="line">244</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;linux/init.h&gt;</span> <span class="comment">/* module_init module_exit */</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;linux/kernel.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;linux/module.h&gt;</span> <span class="comment">/* MODULE_LICENSE */</span></span></span><br><span class="line"></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;linux/kobject.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;linux/slab.h&gt;</span></span></span><br><span class="line"></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;linux/device.h&gt;</span></span></span><br><span class="line"></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&quot;./timestamp_autogenerated.h&quot;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&quot;./version_autogenerated.h&quot;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&quot;./sdrv_common.h&quot;</span></span></span><br><span class="line"></span><br><span class="line"><span class="comment">// #undef PRT</span></span><br><span class="line"><span class="comment">// #undef PRTE</span></span><br><span class="line"><span class="meta">#<span class="keyword">ifndef</span> PRT</span></span><br><span class="line"><span class="meta">#<span class="keyword">define</span> PRT printk</span></span><br><span class="line"><span class="meta">#<span class="keyword">endif</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">ifndef</span> PRTE</span></span><br><span class="line"><span class="meta">#<span class="keyword">define</span> PRTE printk</span></span><br><span class="line"><span class="meta">#<span class="keyword">endif</span></span></span><br><span class="line"></span><br><span class="line"><span class="meta">#<span class="keyword">define</span> SBUS_NAME      <span class="string">&quot;sbus&quot;</span>  <span class="comment">// 总线名称, /sys/bus 中将会创建对应目录，即 /sys/bus/bus-name</span></span></span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * bus 总线的属性变量数据结构，后续实现属性回调函数之后，可以通过 echo/cat 修改这些属性的值</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="keyword">typedef</span> <span class="class"><span class="keyword">struct</span> __<span class="title">SBUS_ATTR_VAR_</span>&#123;</span></span><br><span class="line">    <span class="type">char</span> name_attr[<span class="number">32</span>];</span><br><span class="line">    <span class="type">int</span> data;</span><br><span class="line">&#125;<span class="type">sbus_attr_var_t</span>;</span><br><span class="line"></span><br><span class="line"><span class="type">sbus_attr_var_t</span> sbus_attr = &#123;<span class="number">0</span>&#125;;     <span class="comment">// sbus 的属性变量</span></span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * @brief  sbus_match()</span></span><br><span class="line"><span class="comment"> * @note   负责总线下的设备以及驱动匹配,使用字符串比较的方式，通过对比驱动以及设备的名字来确定是否匹配，</span></span><br><span class="line"><span class="comment"> *         如果相同， 则说明匹配成功。</span></span><br><span class="line"><span class="comment"> * @param [in]</span></span><br><span class="line"><span class="comment"> * @param [out]</span></span><br><span class="line"><span class="comment"> * @retval 匹配成功，返回1；反之，则返回0，这里应该是驱动内部有判断的地方，若是成功不是返回1，则不会执行</span></span><br><span class="line"><span class="comment"> *         驱动中的probe函数</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="type">static</span> <span class="type">int</span> <span class="title function_">sbus_match</span><span class="params">(<span class="keyword">struct</span> device *dev, <span class="keyword">struct</span> device_driver *drv)</span></span><br><span class="line">&#123;</span><br><span class="line">    <span class="type">const</span> <span class="type">char</span> *device_name = dev_name(dev);</span><br><span class="line">    <span class="type">int</span> len = <span class="built_in">strlen</span>(drv-&gt;name);</span><br><span class="line"></span><br><span class="line">	PRT(<span class="string">&quot;dev name is %s, drv name is %s, name len = %d\n&quot;</span>, device_name, drv-&gt;name, len);</span><br><span class="line"></span><br><span class="line">	<span class="keyword">if</span> (!<span class="built_in">strncmp</span>(device_name, drv-&gt;name, len)) </span><br><span class="line">    &#123;</span><br><span class="line">		PRT(<span class="string">&quot;dev is %s &lt;---&gt; drv is %s, match success!\n&quot;</span>, drv-&gt;name, device_name);</span><br><span class="line">		<span class="keyword">return</span> <span class="number">1</span>;</span><br><span class="line">	&#125;</span><br><span class="line">    <span class="comment">//else的话就是没有匹配上</span></span><br><span class="line">	<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * @brief  sbus_probe()</span></span><br><span class="line"><span class="comment"> * @note   设备探测的回调函数，若是驱动中也定义了xxx_probe()函数，则驱动模块加载时</span></span><br><span class="line"><span class="comment"> *         会报  Driver &#x27;xxx&#x27; needs updating - please use bus_type methods</span></span><br><span class="line"><span class="comment"> *         另外，在驱动模块加载时，是优先判断总线中probe函数是否存在，若存在，则会执行总线中的这个probe函数。</span></span><br><span class="line"><span class="comment"> * @param [in]</span></span><br><span class="line"><span class="comment"> * @param [out]</span></span><br><span class="line"><span class="comment"> * @retval </span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="type">static</span> <span class="type">int</span> <span class="title function_">sbus_probe</span><span class="params">(<span class="keyword">struct</span> device *dev)</span></span><br><span class="line">&#123;</span><br><span class="line">    <span class="class"><span class="keyword">struct</span> <span class="title">device_driver</span> *<span class="title">drv</span> =</span> dev-&gt;driver;</span><br><span class="line">    <span class="type">const</span> <span class="type">char</span> *device_name = dev_name(dev);</span><br><span class="line"></span><br><span class="line">    PRT(<span class="string">&quot;device_name=%s driver_name=%s\n&quot;</span>, device_name, drv-&gt;name);</span><br><span class="line">    <span class="keyword">if</span> (drv-&gt;probe)</span><br><span class="line">    &#123;</span><br><span class="line">        drv-&gt;probe(dev); <span class="comment">// 调用驱动的 sdrv_probe() 探测函数</span></span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">// 定义一个新的总线，变量名为 g_sbus，总线结构体中最重要的一个成员，便是 match 回调函数</span></span><br><span class="line"><span class="type">static</span> <span class="class"><span class="keyword">struct</span> <span class="title">bus_type</span> <span class="title">g_sbus</span> =</span> &#123;</span><br><span class="line">	.name = SBUS_NAME,   <span class="comment">// 总线的名称 &quot;sbus&quot;， /sys/bus 中将会创建对应目录，即 /sys/bus/bus-name</span></span><br><span class="line">	.match = sbus_match, <span class="comment">// 设备和驱动程序匹配的回调函数</span></span><br><span class="line">    .probe = sbus_probe, <span class="comment">// 设备探测的回调函数</span></span><br><span class="line">&#125;;</span><br><span class="line">EXPORT_SYMBOL_GPL(g_sbus); <span class="comment">// 导出总线符号，也就是导出 g_sbus 变量，驱动和设备中都会用到</span></span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * @brief  xxx_attr_show()</span></span><br><span class="line"><span class="comment"> * @note   提供show回调函数，这样用户便可以通过cat命令，来查询相关属性参数</span></span><br><span class="line"><span class="comment"> * @param [in]</span></span><br><span class="line"><span class="comment"> * @param [out]</span></span><br><span class="line"><span class="comment"> * @retval </span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="type">static</span> <span class="type">ssize_t</span> <span class="title function_">sbus_attr_name_show</span><span class="params">(<span class="keyword">struct</span> bus_type *bus, <span class="type">char</span> *buf)</span></span><br><span class="line">&#123;</span><br><span class="line">	<span class="type">ssize_t</span> count = <span class="number">0</span>;</span><br><span class="line">    count = <span class="built_in">sprintf</span>(buf, <span class="string">&quot;%s\n&quot;</span>, sbus_attr.name_attr);</span><br><span class="line">    PRT(<span class="string">&quot;bus-&gt;name=%s count=%d\n&quot;</span>, bus-&gt;name, count);</span><br><span class="line"></span><br><span class="line">	<span class="keyword">return</span> count;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * @brief  xxx_attr_store()</span></span><br><span class="line"><span class="comment"> * @note   提供store回调函数，这样用户便可以通过echo命令， 来修改相关属性参数</span></span><br><span class="line"><span class="comment"> * @param [in]</span></span><br><span class="line"><span class="comment"> * @param [out]</span></span><br><span class="line"><span class="comment"> * @retval </span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="type">static</span> <span class="type">ssize_t</span> <span class="title function_">sbus_attr_name_store</span><span class="params">(<span class="keyword">struct</span> bus_type *bus, <span class="type">const</span> <span class="type">char</span> *buf, <span class="type">size_t</span> count)</span></span><br><span class="line">&#123;</span><br><span class="line">    PRT(<span class="string">&quot;bus-&gt;name=%s count=%d\n&quot;</span>, bus-&gt;name, count);</span><br><span class="line">    <span class="built_in">sscanf</span>(buf, <span class="string">&quot;%s\n&quot;</span>, sbus_attr.name_attr);</span><br><span class="line">    <span class="keyword">return</span> count;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">struct</span> <span class="title">bus_attribute</span> <span class="title">sbus_attr_name_var</span> =</span> &#123;</span><br><span class="line">    .attr = &#123;</span><br><span class="line">        .name = <span class="string">&quot;sbus_attr_name&quot;</span>,    <span class="comment">// 属性的名称,将会创建 /sys/bus/bus-name/attr-name 文件，并可以使用 cat/echo 进行操作</span></span><br><span class="line">        .mode = <span class="number">0664</span>,                <span class="comment">// 属性的访问权限</span></span><br><span class="line">    &#125;,</span><br><span class="line">    .show = sbus_attr_name_show,              <span class="comment">// 属性的 show 回调函数</span></span><br><span class="line">    .store = sbus_attr_name_store,            <span class="comment">// 属性的 show 回调函数</span></span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * @brief  xxx_attr_show()</span></span><br><span class="line"><span class="comment"> * @note   提供show回调函数，这样用户便可以通过cat命令，来查询相关属性参数</span></span><br><span class="line"><span class="comment"> * @param [in]</span></span><br><span class="line"><span class="comment"> * @param [out]</span></span><br><span class="line"><span class="comment"> * @retval </span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="type">static</span> <span class="type">ssize_t</span> <span class="title function_">sbus_attr_data_show</span><span class="params">(<span class="keyword">struct</span> bus_type *bus, <span class="type">char</span> *buf)</span></span><br><span class="line">&#123;</span><br><span class="line">	<span class="type">ssize_t</span> count = <span class="number">0</span>;</span><br><span class="line">    count = <span class="built_in">sprintf</span>(buf, <span class="string">&quot;%d\n&quot;</span>, sbus_attr.data);</span><br><span class="line">    PRT(<span class="string">&quot;bus-&gt;name=%s count=%d\n&quot;</span>, bus-&gt;name, count);</span><br><span class="line"></span><br><span class="line">	<span class="keyword">return</span> count;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * @brief  xxx_attr_store()</span></span><br><span class="line"><span class="comment"> * @note   提供store回调函数，这样用户便可以通过echo命令， 来修改相关属性参数</span></span><br><span class="line"><span class="comment"> * @param [in]</span></span><br><span class="line"><span class="comment"> * @param [out]</span></span><br><span class="line"><span class="comment"> * @retval </span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="type">static</span> <span class="type">ssize_t</span> <span class="title function_">sbus_attr_data_store</span><span class="params">(<span class="keyword">struct</span> bus_type *bus, <span class="type">const</span> <span class="type">char</span> *buf, <span class="type">size_t</span> count)</span></span><br><span class="line">&#123;</span><br><span class="line">    PRT(<span class="string">&quot;bus-&gt;name=%s count=%d\n&quot;</span>, bus-&gt;name, count);</span><br><span class="line">    <span class="built_in">sscanf</span>(buf, <span class="string">&quot;%d\n&quot;</span>, &amp;sbus_attr.data);</span><br><span class="line">    <span class="keyword">return</span> count;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">/** </span></span><br><span class="line"><span class="comment"> * 下面这个宏可以简化属性变量的定义，从定义可以看出，这里定义的属性的变量前缀是 bus_attr_，</span></span><br><span class="line"><span class="comment"> * 后面要再拼接name才行</span></span><br><span class="line"><span class="comment"> * </span></span><br><span class="line"><span class="comment"> * #define BUS_ATTR(_name, _mode, _show, _store)</span></span><br><span class="line"><span class="comment"> *	 struct bus_attribute bus_attr_##_name = __ATTR(_name, _mode, _show, _store)</span></span><br><span class="line"><span class="comment"> *</span></span><br><span class="line"><span class="comment"> * 例如下面这个变量使用时，加上前缀 bus_attr_ ，变量名为 bus_attr_sbus_attr_data     </span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line">BUS_ATTR(sbus_attr_data, <span class="number">0664</span>, sbus_attr_data_show, sbus_attr_data_store);</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * @brief  sbus_demo_init()</span></span><br><span class="line"><span class="comment"> * @note   </span></span><br><span class="line"><span class="comment"> * @param [in]</span></span><br><span class="line"><span class="comment"> * @param [out]</span></span><br><span class="line"><span class="comment"> * @retval </span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="type">static</span> __init <span class="type">int</span> <span class="title function_">sbus_demo_init</span><span class="params">(<span class="type">void</span>)</span></span><br><span class="line">&#123;</span><br><span class="line">    <span class="type">int</span> ret = <span class="number">0</span>;</span><br><span class="line">	printk(<span class="string">&quot;*** [%s:%d]Build Time: %s %s, git version:%s ***\n&quot;</span>, __FUNCTION__,</span><br><span class="line">           __LINE__, KERNEL_KO_DATE, KERNEL_KO_TIME, KERNEL_KO_VERSION);</span><br><span class="line">    PRT(<span class="string">&quot;sbus_demo module init!\n&quot;</span>);</span><br><span class="line"></span><br><span class="line">	ret = bus_register(&amp;g_sbus);</span><br><span class="line">    <span class="keyword">if</span>(ret &lt; <span class="number">0</span>)</span><br><span class="line">    &#123;</span><br><span class="line">        PRTE(<span class="string">&quot;bus_register fail!\n&quot;</span>);</span><br><span class="line">        <span class="keyword">goto</span> err_bus_register;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 初始化属性文件控制的变量的值</span></span><br><span class="line">    <span class="built_in">snprintf</span>(sbus_attr.name_attr, <span class="keyword">sizeof</span>(sbus_attr.name_attr), SBUS_NAME);</span><br><span class="line">    sbus_attr.data = <span class="number">1</span>;</span><br><span class="line"></span><br><span class="line">    ret = bus_create_file(&amp;g_sbus, &amp;sbus_attr_name_var);</span><br><span class="line">    <span class="keyword">if</span>(ret &lt; <span class="number">0</span>)</span><br><span class="line">    &#123;</span><br><span class="line">        PRTE(<span class="string">&quot;bus_create_file sbus_attr_name_var fail!ret=%d\n&quot;</span>, ret);</span><br><span class="line">        <span class="keyword">goto</span> err_bus_create_file1;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    ret = bus_create_file(&amp;g_sbus, &amp;bus_attr_sbus_attr_data);</span><br><span class="line">    <span class="keyword">if</span>(ret &lt; <span class="number">0</span>)</span><br><span class="line">    &#123;</span><br><span class="line">        PRTE(<span class="string">&quot;bus_create_file bus_attr_sbus_attr_data fail!ret=%d\n&quot;</span>, ret);</span><br><span class="line">        <span class="keyword">goto</span> err_bus_create_file2;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line"></span><br><span class="line">err_bus_create_file2:</span><br><span class="line">    bus_remove_file(&amp;g_sbus, &amp;sbus_attr_name_var);</span><br><span class="line">err_bus_create_file1:</span><br><span class="line">    bus_unregister(&amp;g_sbus);</span><br><span class="line">err_bus_register:</span><br><span class="line">	<span class="keyword">return</span> ret;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * @brief  sbus_demo_exit()</span></span><br><span class="line"><span class="comment"> * @note   </span></span><br><span class="line"><span class="comment"> * @param [in]</span></span><br><span class="line"><span class="comment"> * @param [out]</span></span><br><span class="line"><span class="comment"> * @retval </span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="type">static</span> __exit <span class="type">void</span> <span class="title function_">sbus_demo_exit</span><span class="params">(<span class="type">void</span>)</span></span><br><span class="line">&#123;</span><br><span class="line">    bus_remove_file(&amp;g_sbus, &amp;bus_attr_sbus_attr_data);</span><br><span class="line">    bus_remove_file(&amp;g_sbus, &amp;sbus_attr_name_var);</span><br><span class="line">	bus_unregister(&amp;g_sbus); <span class="comment">// 取消注册总线</span></span><br><span class="line"></span><br><span class="line">    PRT(<span class="string">&quot;sbus_demo module exit!\n&quot;</span>);</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">module_init(sbus_demo_init); <span class="comment">// 将__init定义的函数指定为驱动的入口函数</span></span><br><span class="line">module_exit(sbus_demo_exit); <span class="comment">// 将__exit定义的函数指定为驱动的出口函数</span></span><br><span class="line"></span><br><span class="line"><span class="comment">/* 模块信息(通过 modinfo xxx.ko 查看) */</span></span><br><span class="line">MODULE_LICENSE(<span class="string">&quot;GPL v2&quot;</span>);            <span class="comment">/* 源码的许可证协议 */</span></span><br><span class="line">MODULE_AUTHOR(<span class="string">&quot;sumu&quot;</span>);               <span class="comment">/* 字符串常量内容为模块作者说明 */</span></span><br><span class="line">MODULE_DESCRIPTION(<span class="string">&quot;Description&quot;</span>);   <span class="comment">/* 字符串常量内容为模块功能说明 */</span></span><br><span class="line">MODULE_ALIAS(<span class="string">&quot;module&#x27;s other name&quot;</span>); <span class="comment">/* 字符串常量内容为模块别名 */</span></span><br><span class="line"></span><br></pre></td></tr></table></figure>
              </div>
            </details>

<h2 id="2-设备"><a href="#2-设备" class="headerlink" title="2. 设备"></a><font size=3>2. 设备</font></h2><h3 id="2-1-设备名称"><a href="#2-1-设备名称" class="headerlink" title="2.1 设备名称"></a><font size=3>2.1 设备名称</font></h3><p>主要是要定义好设备名称：</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">define</span> SDEV_NAME  <span class="string">&quot;sumu-dev&quot;</span>   <span class="comment">// 设备名称, 和驱动中的 匹配名称 相同时就可以匹配对应的驱动</span></span></span><br></pre></td></tr></table></figure>

<h3 id="2-2-xxx-release"><a href="#2-2-xxx-release" class="headerlink" title="2.2 xxx_release()"></a><font size=3>2.2 xxx_release()</font></h3><p>可以在这里实现一个释放函数：</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * @brief  sdev_release()</span></span><br><span class="line"><span class="comment"> * @note   </span></span><br><span class="line"><span class="comment"> * @param [in]</span></span><br><span class="line"><span class="comment"> * @param [out]</span></span><br><span class="line"><span class="comment"> * @retval </span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="type">void</span> <span class="title function_">sdev_release</span><span class="params">(<span class="keyword">struct</span> device *dev)</span></span><br><span class="line">&#123;</span><br><span class="line">    <span class="type">int</span> len = <span class="number">0</span>;</span><br><span class="line"></span><br><span class="line">    <span class="type">const</span> <span class="type">char</span> *device_name = dev_name(dev);</span><br><span class="line">    len = <span class="built_in">strlen</span>(device_name);</span><br><span class="line"></span><br><span class="line">	PRT(<span class="string">&quot;dev name is %s, len = %d\n&quot;</span>, device_name, len);</span><br><span class="line"></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h3 id="2-3-sdevice-demo-c"><a href="#2-3-sdevice-demo-c" class="headerlink" title="2.3 sdevice_demo.c"></a><font size=3>2.3 sdevice_demo.c</font></h3><p>完整源码如下所示：</p>
<details class="folding-tag" blue><summary> 点击查看详情 </summary>
              <div class='content'>
              <figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span class="line">126</span><br><span class="line">127</span><br><span class="line">128</span><br><span class="line">129</span><br><span class="line">130</span><br><span class="line">131</span><br><span class="line">132</span><br><span class="line">133</span><br><span class="line">134</span><br><span class="line">135</span><br><span class="line">136</span><br><span class="line">137</span><br><span class="line">138</span><br><span class="line">139</span><br><span class="line">140</span><br><span class="line">141</span><br><span class="line">142</span><br><span class="line">143</span><br><span class="line">144</span><br><span class="line">145</span><br><span class="line">146</span><br><span class="line">147</span><br><span class="line">148</span><br><span class="line">149</span><br><span class="line">150</span><br><span class="line">151</span><br><span class="line">152</span><br><span class="line">153</span><br><span class="line">154</span><br><span class="line">155</span><br><span class="line">156</span><br><span class="line">157</span><br><span class="line">158</span><br><span class="line">159</span><br><span class="line">160</span><br><span class="line">161</span><br><span class="line">162</span><br><span class="line">163</span><br><span class="line">164</span><br><span class="line">165</span><br><span class="line">166</span><br><span class="line">167</span><br><span class="line">168</span><br><span class="line">169</span><br><span class="line">170</span><br><span class="line">171</span><br><span class="line">172</span><br><span class="line">173</span><br><span class="line">174</span><br><span class="line">175</span><br><span class="line">176</span><br><span class="line">177</span><br><span class="line">178</span><br><span class="line">179</span><br><span class="line">180</span><br><span class="line">181</span><br><span class="line">182</span><br><span class="line">183</span><br><span class="line">184</span><br><span class="line">185</span><br><span class="line">186</span><br><span class="line">187</span><br><span class="line">188</span><br><span class="line">189</span><br><span class="line">190</span><br><span class="line">191</span><br><span class="line">192</span><br><span class="line">193</span><br><span class="line">194</span><br><span class="line">195</span><br><span class="line">196</span><br><span class="line">197</span><br><span class="line">198</span><br><span class="line">199</span><br><span class="line">200</span><br><span class="line">201</span><br><span class="line">202</span><br><span class="line">203</span><br><span class="line">204</span><br><span class="line">205</span><br><span class="line">206</span><br><span class="line">207</span><br><span class="line">208</span><br><span class="line">209</span><br><span class="line">210</span><br><span class="line">211</span><br><span class="line">212</span><br><span class="line">213</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;linux/init.h&gt;</span> <span class="comment">/* module_init module_exit */</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;linux/kernel.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;linux/module.h&gt;</span> <span class="comment">/* MODULE_LICENSE */</span></span></span><br><span class="line"></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;linux/device.h&gt;</span></span></span><br><span class="line"></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&quot;./timestamp_autogenerated.h&quot;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&quot;./version_autogenerated.h&quot;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&quot;./sdrv_common.h&quot;</span></span></span><br><span class="line"></span><br><span class="line"><span class="comment">// #undef PRT</span></span><br><span class="line"><span class="comment">// #undef PRTE</span></span><br><span class="line"><span class="meta">#<span class="keyword">ifndef</span> PRT</span></span><br><span class="line"><span class="meta">#<span class="keyword">define</span> PRT printk</span></span><br><span class="line"><span class="meta">#<span class="keyword">endif</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">ifndef</span> PRTE</span></span><br><span class="line"><span class="meta">#<span class="keyword">define</span> PRTE printk</span></span><br><span class="line"><span class="meta">#<span class="keyword">endif</span></span></span><br><span class="line"></span><br><span class="line"><span class="meta">#<span class="keyword">define</span> SDEV_NAME  <span class="string">&quot;sumu-dev&quot;</span>   <span class="comment">// 设备名称, 和驱动中的 匹配名称 相同时就可以匹配对应的驱动</span></span></span><br><span class="line">                                <span class="comment">// /sys/bus/bus-name/devices 中将会创建对应目录，即 /sys/bus/bus-name/devices/device-name</span></span><br><span class="line">                                <span class="comment">// /sys/devices 中也会创建对应的目录，即/sys/devices/device-name</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">extern</span> <span class="class"><span class="keyword">struct</span> <span class="title">bus_type</span> <span class="title">g_sbus</span>;</span>  <span class="comment">// g_sbus 操作函数集的那个全局变量，包含了match函数</span></span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * sdev设备的属性变量数据结构，后续实现属性回调函数之后，可以通过 echo/cat 修改这些属性的值</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="keyword">typedef</span> <span class="class"><span class="keyword">struct</span> __<span class="title">SDEV_ATTR_VAR_</span>&#123;</span></span><br><span class="line">    <span class="type">char</span> name_attr[<span class="number">32</span>];</span><br><span class="line">    <span class="type">int</span> data;</span><br><span class="line">&#125;<span class="type">sdev_attr_var_t</span>;</span><br><span class="line"></span><br><span class="line"><span class="type">sdev_attr_var_t</span> sdev_attr = &#123;<span class="number">0</span>&#125;;     <span class="comment">// sdevice 的属性</span></span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * @brief  sdev_release()</span></span><br><span class="line"><span class="comment"> * @note   </span></span><br><span class="line"><span class="comment"> * @param [in]</span></span><br><span class="line"><span class="comment"> * @param [out]</span></span><br><span class="line"><span class="comment"> * @retval </span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="type">void</span> <span class="title function_">sdev_release</span><span class="params">(<span class="keyword">struct</span> device *dev)</span></span><br><span class="line">&#123;</span><br><span class="line">    <span class="type">int</span> len = <span class="number">0</span>;</span><br><span class="line"></span><br><span class="line">    <span class="type">const</span> <span class="type">char</span> *device_name = dev_name(dev);</span><br><span class="line">    len = <span class="built_in">strlen</span>(device_name);</span><br><span class="line"></span><br><span class="line">	PRT(<span class="string">&quot;dev name is %s, len = %d\n&quot;</span>, device_name, len);</span><br><span class="line"></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="type">static</span> <span class="class"><span class="keyword">struct</span> <span class="title">device</span> <span class="title">sdev</span> =</span> &#123;</span><br><span class="line">	.init_name = SDEV_NAME,    <span class="comment">// 设备的初始化名称，/sys/bus/bus-name/devices 中将会创建对应的目录，即 /sys/bus/bus-name/devices/device-name</span></span><br><span class="line">	.bus = &amp;g_sbus,            <span class="comment">// 所属总线</span></span><br><span class="line">	.release = sdev_release,   <span class="comment">// 设备的释放回调函数</span></span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * @brief  xxx_attr_show()</span></span><br><span class="line"><span class="comment"> * @note   提供show回调函数，这样用户便可以通过cat命令， 来查询相关属性参数</span></span><br><span class="line"><span class="comment"> * @param [in]</span></span><br><span class="line"><span class="comment"> * @param [out]</span></span><br><span class="line"><span class="comment"> * @retval </span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="type">static</span> <span class="type">ssize_t</span> <span class="title function_">sdev_attr_name_show</span><span class="params">(<span class="keyword">struct</span> device *dev, <span class="keyword">struct</span> device_attribute *attr, <span class="type">char</span> *buf)</span></span><br><span class="line">&#123;</span><br><span class="line">	<span class="type">ssize_t</span> count = <span class="number">0</span>;</span><br><span class="line">    count = <span class="built_in">sprintf</span>(buf, <span class="string">&quot;%s\n&quot;</span>, sdev_attr.name_attr);</span><br><span class="line">    PRT(<span class="string">&quot;attr-&gt;name=%s count=%d\n&quot;</span>, attr-&gt;attr.name, count);</span><br><span class="line"></span><br><span class="line">	<span class="keyword">return</span> count;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * @brief  xxx_attr_store()</span></span><br><span class="line"><span class="comment"> * @note   提供store回调函数，这样用户便可以通过echo命令， 来修改相关属性参数</span></span><br><span class="line"><span class="comment"> * @param [in]</span></span><br><span class="line"><span class="comment"> * @param [out]</span></span><br><span class="line"><span class="comment"> * @retval </span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="type">static</span> <span class="type">ssize_t</span> <span class="title function_">sdev_attr_name_store</span><span class="params">(<span class="keyword">struct</span> device * dev, <span class="keyword">struct</span> device_attribute * attr, <span class="type">const</span> <span class="type">char</span> *buf, <span class="type">size_t</span> count)</span></span><br><span class="line">&#123;</span><br><span class="line">    PRT(<span class="string">&quot;attr-&gt;name=%s count=%d\n&quot;</span>, attr-&gt;attr.name, count);</span><br><span class="line">    <span class="built_in">sscanf</span>(buf, <span class="string">&quot;%s\n&quot;</span>, sdev_attr.name_attr);</span><br><span class="line">    <span class="keyword">return</span> count;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">struct</span> <span class="title">device_attribute</span> <span class="title">sdev_attr_name_var</span> =</span> &#123;</span><br><span class="line">    .attr = &#123;</span><br><span class="line">        .name = <span class="string">&quot;sdev_attr_name&quot;</span>,    <span class="comment">// 属性的名称,将会显示在 /sys/bus/bus-name/devices/device-name/ 中，并可以使用cat/echo 进行操作</span></span><br><span class="line">        .mode = <span class="number">0664</span>,                <span class="comment">// 属性的访问权限</span></span><br><span class="line">    &#125;,</span><br><span class="line">    .show = sdev_attr_name_show,              <span class="comment">// 属性的 show 回调函数</span></span><br><span class="line">    .store = sdev_attr_name_store,            <span class="comment">// 属性的 show 回调函数</span></span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * @brief  xxx_attr_show()</span></span><br><span class="line"><span class="comment"> * @note   提供show回调函数，这样用户便可以通过cat命令， 来查询相关属性参数</span></span><br><span class="line"><span class="comment"> * @param [in]</span></span><br><span class="line"><span class="comment"> * @param [out]</span></span><br><span class="line"><span class="comment"> * @retval </span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="type">static</span> <span class="type">ssize_t</span> <span class="title function_">sdev_attr_data_show</span><span class="params">(<span class="keyword">struct</span> device *dev, <span class="keyword">struct</span> device_attribute *attr, <span class="type">char</span> *buf)</span></span><br><span class="line">&#123;</span><br><span class="line">	<span class="type">ssize_t</span> count = <span class="number">0</span>;</span><br><span class="line">    count = <span class="built_in">sprintf</span>(buf, <span class="string">&quot;%d\n&quot;</span>, sdev_attr.data);</span><br><span class="line">    PRT(<span class="string">&quot;attr-&gt;name=%s count=%d\n&quot;</span>, attr-&gt;attr.name, count);</span><br><span class="line"></span><br><span class="line">	<span class="keyword">return</span> count;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * @brief  xxx_attr_store()</span></span><br><span class="line"><span class="comment"> * @note   提供store回调函数，这样用户便可以通过echo命令， 来修改相关属性参数</span></span><br><span class="line"><span class="comment"> * @param [in]</span></span><br><span class="line"><span class="comment"> * @param [out]</span></span><br><span class="line"><span class="comment"> * @retval </span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="type">static</span> <span class="type">ssize_t</span> <span class="title function_">sdev_attr_data_store</span><span class="params">(<span class="keyword">struct</span> device * dev, <span class="keyword">struct</span> device_attribute * attr, <span class="type">const</span> <span class="type">char</span> *buf, <span class="type">size_t</span> count)</span></span><br><span class="line">&#123;</span><br><span class="line">    PRT(<span class="string">&quot;attr-&gt;name=%s count=%d\n&quot;</span>, attr-&gt;attr.name, count);</span><br><span class="line">    <span class="built_in">sscanf</span>(buf, <span class="string">&quot;%d\n&quot;</span>, &amp;sdev_attr.data);</span><br><span class="line">    <span class="keyword">return</span> count;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">/** </span></span><br><span class="line"><span class="comment"> * 下面这个宏可以简化属性变量的定义，从定义可以看出，这里定义的属性的变量前缀是 dev_attr_，</span></span><br><span class="line"><span class="comment"> * 后面要再拼接name才行</span></span><br><span class="line"><span class="comment"> * </span></span><br><span class="line"><span class="comment"> * #define DEVICE_ATTR(_name, _mode, _show, _store) </span></span><br><span class="line"><span class="comment"> *	 struct device_attribute dev_attr_##_name = __ATTR(_name, _mode, _show, _store)</span></span><br><span class="line"><span class="comment"> *</span></span><br><span class="line"><span class="comment"> * 例如下面这个变量使用时，加上前缀 dev_attr_ ，变量名为 dev_attr_sdev_attr_data     </span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line">DEVICE_ATTR(sdev_attr_data, <span class="number">0664</span>, sdev_attr_data_show, sdev_attr_data_store);</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * @brief  sdev_demo_init()</span></span><br><span class="line"><span class="comment"> * @note   设备结构体以及属性文件结构体注册</span></span><br><span class="line"><span class="comment"> * @param [in]</span></span><br><span class="line"><span class="comment"> * @param [out]</span></span><br><span class="line"><span class="comment"> * @retval </span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="type">static</span> __init <span class="type">int</span> <span class="title function_">sdev_demo_init</span><span class="params">(<span class="type">void</span>)</span></span><br><span class="line">&#123;</span><br><span class="line">    <span class="type">int</span> ret = <span class="number">0</span>;</span><br><span class="line">	printk(<span class="string">&quot;*** [%s:%d]Build Time: %s %s, git version:%s ***\n&quot;</span>, __FUNCTION__,</span><br><span class="line">           __LINE__, KERNEL_KO_DATE, KERNEL_KO_TIME, KERNEL_KO_VERSION);</span><br><span class="line">    PRT(<span class="string">&quot;sdev_demo module init!\n&quot;</span>);</span><br><span class="line"></span><br><span class="line">	ret = device_register(&amp;sdev);</span><br><span class="line">    <span class="keyword">if</span>(ret &lt; <span class="number">0</span>)</span><br><span class="line">    &#123;</span><br><span class="line">        PRTE(<span class="string">&quot;device_register fail! ret = %d\n&quot;</span>, ret);</span><br><span class="line">        <span class="keyword">goto</span> err_device_register;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 初始化属性文件控制的变量的值</span></span><br><span class="line">    <span class="built_in">snprintf</span>(sdev_attr.name_attr, <span class="keyword">sizeof</span>(sdev_attr.name_attr), SDEV_NAME);</span><br><span class="line">    sdev_attr.data = <span class="number">1</span>;</span><br><span class="line"></span><br><span class="line">	ret = device_create_file(&amp;sdev, &amp;sdev_attr_name_var);</span><br><span class="line">    <span class="keyword">if</span>(ret &lt; <span class="number">0</span>)</span><br><span class="line">    &#123;</span><br><span class="line">        PRTE(<span class="string">&quot;device_create_file sdev_attr_name_var fail!ret=%d\n&quot;</span>, ret);</span><br><span class="line">        <span class="keyword">goto</span> err_device_create_file1;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    ret = device_create_file(&amp;sdev, &amp;dev_attr_sdev_attr_data);</span><br><span class="line">    <span class="keyword">if</span>(ret &lt; <span class="number">0</span>)</span><br><span class="line">    &#123;</span><br><span class="line">        PRTE(<span class="string">&quot;device_create_file dev_attr_sdev_attr_data fail!ret=%d\n&quot;</span>, ret);</span><br><span class="line">        <span class="keyword">goto</span> err_device_create_file2;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">	<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line"></span><br><span class="line">err_device_create_file2:</span><br><span class="line">    device_remove_file(&amp;sdev, &amp;sdev_attr_name_var);</span><br><span class="line">err_device_create_file1:</span><br><span class="line">    device_unregister(&amp;sdev);</span><br><span class="line">err_device_register:</span><br><span class="line">    <span class="keyword">return</span> ret;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * @brief  sdev_demo_exit</span></span><br><span class="line"><span class="comment"> * @note   设备结构体以及属性文件结构体注销。</span></span><br><span class="line"><span class="comment"> * @param [in]</span></span><br><span class="line"><span class="comment"> * @param [out]</span></span><br><span class="line"><span class="comment"> * @retval </span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="type">static</span> __exit <span class="type">void</span> <span class="title function_">sdev_demo_exit</span><span class="params">(<span class="type">void</span>)</span></span><br><span class="line">&#123;</span><br><span class="line">    device_remove_file(&amp;sdev, &amp;dev_attr_sdev_attr_data);</span><br><span class="line">    device_remove_file(&amp;sdev, &amp;sdev_attr_name_var);</span><br><span class="line">	device_unregister(&amp;sdev); <span class="comment">// 取消注册设备</span></span><br><span class="line"></span><br><span class="line">    PRT(<span class="string">&quot;sdev_demo module exit!\n&quot;</span>);</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">module_init(sdev_demo_init); <span class="comment">// 将__init定义的函数指定为驱动的入口函数</span></span><br><span class="line">module_exit(sdev_demo_exit); <span class="comment">// 将__exit定义的函数指定为驱动的出口函数</span></span><br><span class="line"></span><br><span class="line"><span class="comment">/* 模块信息(通过 modinfo xxx.ko 查看) */</span></span><br><span class="line">MODULE_LICENSE(<span class="string">&quot;GPL v2&quot;</span>);            <span class="comment">/* 源码的许可证协议 */</span></span><br><span class="line">MODULE_AUTHOR(<span class="string">&quot;sumu&quot;</span>);               <span class="comment">/* 字符串常量内容为模块作者说明 */</span></span><br><span class="line">MODULE_DESCRIPTION(<span class="string">&quot;Description&quot;</span>);   <span class="comment">/* 字符串常量内容为模块功能说明 */</span></span><br><span class="line">MODULE_ALIAS(<span class="string">&quot;module&#x27;s other name&quot;</span>); <span class="comment">/* 字符串常量内容为模块别名 */</span></span><br><span class="line"></span><br></pre></td></tr></table></figure>
              </div>
            </details>

<h2 id="3-驱动"><a href="#3-驱动" class="headerlink" title="3. 驱动"></a><font size=3>3. 驱动</font></h2><h3 id="3-1-驱动名称"><a href="#3-1-驱动名称" class="headerlink" title="3.1 驱动名称"></a><font size=3>3.1 驱动名称</font></h3><p>需要注意的是，驱动的名称要和设备名称一致，这样我们才能在对应的总线中通过xxx_match()函数，实现驱动和设备的匹配：</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">define</span> SDRV_MATCH_NAME  <span class="string">&quot;sumu-dev&quot;</span>    <span class="comment">// 和 设备匹配的名字，设备名为 sumu-dev 的才能匹配成功</span></span></span><br></pre></td></tr></table></figure>

<h3 id="3-2-xxx-probe"><a href="#3-2-xxx-probe" class="headerlink" title="3.2 xxx_probe()"></a><font size=3>3.2 xxx_probe()</font></h3><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * @brief  sdrv_probe()</span></span><br><span class="line"><span class="comment"> * @note   驱动程序的探测函数</span></span><br><span class="line"><span class="comment"> * @param [in]</span></span><br><span class="line"><span class="comment"> * @param [out]</span></span><br><span class="line"><span class="comment"> * @retval </span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="type">static</span> <span class="type">int</span> <span class="title function_">sdrv_probe</span><span class="params">(<span class="keyword">struct</span> device *dev)</span></span><br><span class="line">&#123;</span><br><span class="line">    <span class="type">int</span> len = <span class="number">0</span>;</span><br><span class="line"></span><br><span class="line">    <span class="type">const</span> <span class="type">char</span> *device_name = dev_name(dev);</span><br><span class="line">    len = <span class="built_in">strlen</span>(device_name);</span><br><span class="line"></span><br><span class="line">	PRT(<span class="string">&quot;dev name is %s, len = %d\n&quot;</span>, device_name, len);</span><br><span class="line">	<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>其实驱动自己的探测函数实现之后，在加载的时候反而会有一个警告，后面再分析。</p>
<h3 id="3-3-xxx-remove"><a href="#3-3-xxx-remove" class="headerlink" title="3.3 xxx_remove()"></a><font size=3>3.3 xxx_remove()</font></h3><p>可以再实现一个remove函数，用于清理资源：</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * @brief  sdrv_remove()</span></span><br><span class="line"><span class="comment"> * @note   驱动程序的移除函数</span></span><br><span class="line"><span class="comment"> * @param [in]</span></span><br><span class="line"><span class="comment"> * @param [out]</span></span><br><span class="line"><span class="comment"> * @retval </span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="type">static</span> <span class="type">int</span> <span class="title function_">sdrv_remove</span><span class="params">(<span class="keyword">struct</span> device *dev)</span></span><br><span class="line">&#123;</span><br><span class="line">    <span class="type">int</span> len = <span class="number">0</span>;</span><br><span class="line"></span><br><span class="line">    <span class="type">const</span> <span class="type">char</span> *device_name = dev_name(dev);</span><br><span class="line">    len = <span class="built_in">strlen</span>(device_name);</span><br><span class="line"></span><br><span class="line">	PRT(<span class="string">&quot;dev name is %s, len = %d\n&quot;</span>, device_name, len);</span><br><span class="line">	<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h3 id="3-4-sdriver-demo-c"><a href="#3-4-sdriver-demo-c" class="headerlink" title="3.4 sdriver_demo.c"></a><font size=3>3.4 sdriver_demo.c</font></h3><p>完整源码如下：</p>
<details class="folding-tag" blue><summary> 点击查看详情 </summary>
              <div class='content'>
              <figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span class="line">126</span><br><span class="line">127</span><br><span class="line">128</span><br><span class="line">129</span><br><span class="line">130</span><br><span class="line">131</span><br><span class="line">132</span><br><span class="line">133</span><br><span class="line">134</span><br><span class="line">135</span><br><span class="line">136</span><br><span class="line">137</span><br><span class="line">138</span><br><span class="line">139</span><br><span class="line">140</span><br><span class="line">141</span><br><span class="line">142</span><br><span class="line">143</span><br><span class="line">144</span><br><span class="line">145</span><br><span class="line">146</span><br><span class="line">147</span><br><span class="line">148</span><br><span class="line">149</span><br><span class="line">150</span><br><span class="line">151</span><br><span class="line">152</span><br><span class="line">153</span><br><span class="line">154</span><br><span class="line">155</span><br><span class="line">156</span><br><span class="line">157</span><br><span class="line">158</span><br><span class="line">159</span><br><span class="line">160</span><br><span class="line">161</span><br><span class="line">162</span><br><span class="line">163</span><br><span class="line">164</span><br><span class="line">165</span><br><span class="line">166</span><br><span class="line">167</span><br><span class="line">168</span><br><span class="line">169</span><br><span class="line">170</span><br><span class="line">171</span><br><span class="line">172</span><br><span class="line">173</span><br><span class="line">174</span><br><span class="line">175</span><br><span class="line">176</span><br><span class="line">177</span><br><span class="line">178</span><br><span class="line">179</span><br><span class="line">180</span><br><span class="line">181</span><br><span class="line">182</span><br><span class="line">183</span><br><span class="line">184</span><br><span class="line">185</span><br><span class="line">186</span><br><span class="line">187</span><br><span class="line">188</span><br><span class="line">189</span><br><span class="line">190</span><br><span class="line">191</span><br><span class="line">192</span><br><span class="line">193</span><br><span class="line">194</span><br><span class="line">195</span><br><span class="line">196</span><br><span class="line">197</span><br><span class="line">198</span><br><span class="line">199</span><br><span class="line">200</span><br><span class="line">201</span><br><span class="line">202</span><br><span class="line">203</span><br><span class="line">204</span><br><span class="line">205</span><br><span class="line">206</span><br><span class="line">207</span><br><span class="line">208</span><br><span class="line">209</span><br><span class="line">210</span><br><span class="line">211</span><br><span class="line">212</span><br><span class="line">213</span><br><span class="line">214</span><br><span class="line">215</span><br><span class="line">216</span><br><span class="line">217</span><br><span class="line">218</span><br><span class="line">219</span><br><span class="line">220</span><br><span class="line">221</span><br><span class="line">222</span><br><span class="line">223</span><br><span class="line">224</span><br><span class="line">225</span><br><span class="line">226</span><br><span class="line">227</span><br><span class="line">228</span><br><span class="line">229</span><br><span class="line">230</span><br><span class="line">231</span><br><span class="line">232</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;linux/init.h&gt;</span> <span class="comment">/* module_init module_exit */</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;linux/kernel.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;linux/module.h&gt;</span> <span class="comment">/* MODULE_LICENSE */</span></span></span><br><span class="line"></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;linux/device.h&gt;</span></span></span><br><span class="line"></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&quot;./timestamp_autogenerated.h&quot;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&quot;./version_autogenerated.h&quot;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&quot;./sdrv_common.h&quot;</span></span></span><br><span class="line"></span><br><span class="line"><span class="comment">// #undef PRT</span></span><br><span class="line"><span class="comment">// #undef PRTE</span></span><br><span class="line"><span class="meta">#<span class="keyword">ifndef</span> PRT</span></span><br><span class="line"><span class="meta">#<span class="keyword">define</span> PRT printk</span></span><br><span class="line"><span class="meta">#<span class="keyword">endif</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">ifndef</span> PRTE</span></span><br><span class="line"><span class="meta">#<span class="keyword">define</span> PRTE printk</span></span><br><span class="line"><span class="meta">#<span class="keyword">endif</span></span></span><br><span class="line"></span><br><span class="line"><span class="meta">#<span class="keyword">define</span> SDRV_MATCH_NAME  <span class="string">&quot;sumu-dev&quot;</span>    <span class="comment">// 和 设备匹配的名字，设备名为 sumu-dev 的才能匹配成功</span></span></span><br><span class="line">                                       <span class="comment">// 这个会作为驱动的名字， /sys/bus/bus-name/drivers 中将会创建对应目录，即 /sys/bus/bus-name/drivers/driver-name</span></span><br><span class="line"><span class="keyword">extern</span> <span class="class"><span class="keyword">struct</span> <span class="title">bus_type</span> <span class="title">g_sbus</span>;</span>         <span class="comment">// g_sbus 操作函数集的那个全局变量，包含了 match 函数</span></span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * sdrv驱动的属性变量数据结构，后续实现属性回调函数之后，可以通过 echo/cat 修改这些属性的值</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="keyword">typedef</span> <span class="class"><span class="keyword">struct</span> __<span class="title">SDRV_ATTR_VAR_</span>&#123;</span></span><br><span class="line">    <span class="type">char</span> name_attr[<span class="number">32</span>];</span><br><span class="line">    <span class="type">int</span> data;</span><br><span class="line">&#125;<span class="type">sdrv_attr_var_t</span>;</span><br><span class="line"></span><br><span class="line"><span class="type">sdrv_attr_var_t</span> sdrv_attr = &#123;<span class="number">0</span>&#125;;     <span class="comment">// sdriver 的属性</span></span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * @brief  sdrv_probe()</span></span><br><span class="line"><span class="comment"> * @note   驱动程序的探测函数，若是总线模块中也定义了xxx_probe()函数，则驱动模块加载时</span></span><br><span class="line"><span class="comment"> *         会报  Driver &#x27;xxx&#x27; needs updating - please use bus_type methods</span></span><br><span class="line"><span class="comment"> *         另外，在驱动模块加载时，是优先判断总线中probe函数是否存在，若存在，则会执行总线中的这个probe函数。</span></span><br><span class="line"><span class="comment"> *         若总线不存在probe函数，则会执行驱动中的这个probe函数。</span></span><br><span class="line"><span class="comment"> * @param [in]</span></span><br><span class="line"><span class="comment"> * @param [out]</span></span><br><span class="line"><span class="comment"> * @retval </span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="type">static</span> <span class="type">int</span> <span class="title function_">sdrv_probe</span><span class="params">(<span class="keyword">struct</span> device *dev)</span></span><br><span class="line">&#123;</span><br><span class="line">    <span class="type">int</span> len = <span class="number">0</span>;</span><br><span class="line"></span><br><span class="line">    <span class="type">const</span> <span class="type">char</span> *device_name = dev_name(dev);</span><br><span class="line">    len = <span class="built_in">strlen</span>(device_name);</span><br><span class="line"></span><br><span class="line">	PRT(<span class="string">&quot;dev name is %s, len = %d\n&quot;</span>, device_name, len);</span><br><span class="line">	<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * @brief  sdrv_remove()</span></span><br><span class="line"><span class="comment"> * @note   驱动程序的移除函数</span></span><br><span class="line"><span class="comment"> * @param [in]</span></span><br><span class="line"><span class="comment"> * @param [out]</span></span><br><span class="line"><span class="comment"> * @retval </span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="type">static</span> <span class="type">int</span> <span class="title function_">sdrv_remove</span><span class="params">(<span class="keyword">struct</span> device *dev)</span></span><br><span class="line">&#123;</span><br><span class="line">    <span class="type">int</span> len = <span class="number">0</span>;</span><br><span class="line"></span><br><span class="line">    <span class="type">const</span> <span class="type">char</span> *device_name = dev_name(dev);</span><br><span class="line">    len = <span class="built_in">strlen</span>(device_name);</span><br><span class="line"></span><br><span class="line">	PRT(<span class="string">&quot;dev name is %s, len = %d\n&quot;</span>, device_name, len);</span><br><span class="line">	<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">// 定义一个驱动结构体sdrv，名字需要和设备的名字相同，否则就不能成功匹配</span></span><br><span class="line"><span class="type">static</span> <span class="class"><span class="keyword">struct</span> <span class="title">device_driver</span> <span class="title">sdrv</span> =</span> &#123;</span><br><span class="line">	.name = SDRV_MATCH_NAME,      <span class="comment">// 驱动程序的名称, /sys/bus/bus-name/drivers 中将会创建对应目录，即 /sys/bus/bus-name/drivers/driver-name</span></span><br><span class="line">	.bus = &amp;g_sbus,               <span class="comment">// 该驱动挂载在已经注册好的总线 bus-name 下。</span></span><br><span class="line">	.probe = sdrv_probe,          <span class="comment">// 驱动程序的探测函数</span></span><br><span class="line">	.remove = sdrv_remove,        <span class="comment">// 当注销驱动时，需要关闭物理设备的某些功能等</span></span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * @brief  xxx_attr_show()</span></span><br><span class="line"><span class="comment"> * @note   提供show回调函数，这样用户便可以通过cat命令， 来查询相关属性参数</span></span><br><span class="line"><span class="comment"> * @param [in]</span></span><br><span class="line"><span class="comment"> * @param [out]</span></span><br><span class="line"><span class="comment"> * @retval </span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="type">static</span> <span class="type">ssize_t</span> <span class="title function_">sdrv_attr_name_show</span><span class="params">(<span class="keyword">struct</span> device_driver *driver, <span class="type">char</span> *buf)</span></span><br><span class="line">&#123;</span><br><span class="line">    <span class="type">ssize_t</span> count = <span class="number">0</span>;</span><br><span class="line">    count = <span class="built_in">sprintf</span>(buf, <span class="string">&quot;%s\n&quot;</span>, sdrv_attr.name_attr);</span><br><span class="line">    PRT(<span class="string">&quot;device_driver-&gt;name=%s count=%d\n&quot;</span>, driver-&gt;name, count);</span><br><span class="line">    <span class="keyword">return</span> count;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * @brief  xxx_attr_store()</span></span><br><span class="line"><span class="comment"> * @note   提供store回调函数，这样用户便可以通过echo命令， 来修改相关属性参数</span></span><br><span class="line"><span class="comment"> * @param [in]</span></span><br><span class="line"><span class="comment"> * @param [out]</span></span><br><span class="line"><span class="comment"> * @retval </span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="type">static</span> <span class="type">ssize_t</span> <span class="title function_">sdrv_attr_name_store</span><span class="params">(<span class="keyword">struct</span> device_driver *driver, <span class="type">const</span> <span class="type">char</span> *buf, <span class="type">size_t</span> count)</span></span><br><span class="line">&#123;</span><br><span class="line">    PRT(<span class="string">&quot;device_driver-&gt;name=%s count=%d\n&quot;</span>, driver-&gt;name, count);</span><br><span class="line">    <span class="built_in">sscanf</span>(buf, <span class="string">&quot;%s\n&quot;</span>, sdrv_attr.name_attr);</span><br><span class="line">    <span class="keyword">return</span> count;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">struct</span> <span class="title">driver_attribute</span> <span class="title">sdrv_attr_name_var</span> =</span> &#123;</span><br><span class="line">    .attr = &#123;</span><br><span class="line">        .name = <span class="string">&quot;sdrv_attr_name&quot;</span>,    <span class="comment">// 属性的名称,将会显示在 /sys/bus/bus-name/drivers/driver-name 下，并可以使用cat/echo 进行操作</span></span><br><span class="line">        .mode = <span class="number">0664</span>,                <span class="comment">// 属性的访问权限</span></span><br><span class="line">    &#125;,</span><br><span class="line">    .show = sdrv_attr_name_show,     <span class="comment">// 属性的 show 回调函数</span></span><br><span class="line">    .store = sdrv_attr_name_store,   <span class="comment">// 属性的 show 回调函数</span></span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * @brief  xxx_attr_show()</span></span><br><span class="line"><span class="comment"> * @note   提供show回调函数，这样用户便可以通过cat命令， 来查询相关属性参数</span></span><br><span class="line"><span class="comment"> * @param [in]</span></span><br><span class="line"><span class="comment"> * @param [out]</span></span><br><span class="line"><span class="comment"> * @retval </span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="type">static</span> <span class="type">ssize_t</span> <span class="title function_">sdrv_attr_data_show</span><span class="params">(<span class="keyword">struct</span> device_driver *driver, <span class="type">char</span> *buf)</span></span><br><span class="line">&#123;</span><br><span class="line">    <span class="type">ssize_t</span> count = <span class="number">0</span>;</span><br><span class="line">    count = <span class="built_in">sprintf</span>(buf, <span class="string">&quot;%d\n&quot;</span>, sdrv_attr.data);</span><br><span class="line">    PRT(<span class="string">&quot;device_driver-&gt;name=%s count=%d\n&quot;</span>, driver-&gt;name, count);</span><br><span class="line">    <span class="keyword">return</span> count;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * @brief  xxx_attr_store()</span></span><br><span class="line"><span class="comment"> * @note   提供store回调函数，这样用户便可以通过echo命令， 来修改相关属性参数</span></span><br><span class="line"><span class="comment"> * @param [in]</span></span><br><span class="line"><span class="comment"> * @param [out]</span></span><br><span class="line"><span class="comment"> * @retval </span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="type">static</span> <span class="type">ssize_t</span> <span class="title function_">sdrv_attr_data_store</span><span class="params">(<span class="keyword">struct</span> device_driver *driver, <span class="type">const</span> <span class="type">char</span> *buf, <span class="type">size_t</span> count)</span></span><br><span class="line">&#123;</span><br><span class="line">    PRT(<span class="string">&quot;device_driver-&gt;name=%s count=%d\n&quot;</span>, driver-&gt;name, count);</span><br><span class="line">    <span class="built_in">sscanf</span>(buf, <span class="string">&quot;%d\n&quot;</span>, &amp;sdrv_attr.data);</span><br><span class="line">    <span class="keyword">return</span> count;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">/** </span></span><br><span class="line"><span class="comment"> * 下面这个宏可以简化属性变量的定义，从定义可以看出，这里定义的属性的变量前缀是 driver_attr_，</span></span><br><span class="line"><span class="comment"> * 后面要再拼接name才行</span></span><br><span class="line"><span class="comment"> * </span></span><br><span class="line"><span class="comment"> * #define DRIVER_ATTR_RW(_name) </span></span><br><span class="line"><span class="comment"> *	 struct device_attribute dev_attr_##_name = __ATTR(_name, _mode, _show, _store)</span></span><br><span class="line"><span class="comment"> * #define __ATTR_RW(_name) __ATTR(_name, 0644, _name##_show, _name##_store)</span></span><br><span class="line"><span class="comment"> * </span></span><br><span class="line"><span class="comment"> * 例如下面这个变量使用时，加上前缀 driver_attr_ ，变量名为 driver_attr_sdrv_attr_name     </span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line">DRIVER_ATTR_RW(sdrv_attr_data);</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * @brief  sdrv_demo_init</span></span><br><span class="line"><span class="comment"> * @note   调用driver_register函数注册我们的驱动</span></span><br><span class="line"><span class="comment"> * @param [in]</span></span><br><span class="line"><span class="comment"> * @param [out]</span></span><br><span class="line"><span class="comment"> * @retval </span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="type">static</span> __init <span class="type">int</span> <span class="title function_">sdrv_demo_init</span><span class="params">(<span class="type">void</span>)</span></span><br><span class="line">&#123;</span><br><span class="line">    <span class="type">int</span> ret = <span class="number">0</span>;</span><br><span class="line">	printk(<span class="string">&quot;*** [%s:%d]Build Time: %s %s, git version:%s ***\n&quot;</span>, __FUNCTION__,</span><br><span class="line">           __LINE__, KERNEL_KO_DATE, KERNEL_KO_TIME, KERNEL_KO_VERSION);</span><br><span class="line">    PRT(<span class="string">&quot;sdrv_demo module init!\n&quot;</span>);</span><br><span class="line"></span><br><span class="line">	ret = driver_register(&amp;sdrv);</span><br><span class="line">    <span class="keyword">if</span>(ret &lt; <span class="number">0</span>)</span><br><span class="line">    &#123;</span><br><span class="line">        PRTE(<span class="string">&quot;driver_register fail!\n&quot;</span>);</span><br><span class="line">        <span class="keyword">goto</span> err_driver_register;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 初始化属性文件控制的变量的值</span></span><br><span class="line">    <span class="built_in">snprintf</span>(sdrv_attr.name_attr, <span class="keyword">sizeof</span>(sdrv_attr.name_attr), SDRV_MATCH_NAME);</span><br><span class="line">    sdrv_attr.data = <span class="number">1</span>;</span><br><span class="line"></span><br><span class="line">	ret = driver_create_file(&amp;sdrv, &amp;sdrv_attr_name_var);</span><br><span class="line">    <span class="keyword">if</span>(ret &lt; <span class="number">0</span>)</span><br><span class="line">    &#123;</span><br><span class="line">        PRTE(<span class="string">&quot;driver_create_file sdrv_attr_name_var fail!ret=%d\n&quot;</span>, ret);</span><br><span class="line">        <span class="keyword">goto</span> err_driver_create_file1;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    ret = driver_create_file(&amp;sdrv, &amp;driver_attr_sdrv_attr_data);</span><br><span class="line">    <span class="keyword">if</span>(ret &lt; <span class="number">0</span>)</span><br><span class="line">    &#123;</span><br><span class="line">        PRTE(<span class="string">&quot;driver_create_file driver_attr_sdrv_attr_data fail!ret=%d\n&quot;</span>, ret);</span><br><span class="line">        <span class="keyword">goto</span> err_driver_create_file2;</span><br><span class="line">    &#125;</span><br><span class="line">	<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line"></span><br><span class="line">err_driver_create_file2:</span><br><span class="line">    driver_remove_file(&amp;sdrv, &amp;sdrv_attr_name_var);</span><br><span class="line">err_driver_create_file1:</span><br><span class="line">    driver_unregister(&amp;sdrv);</span><br><span class="line">err_driver_register:</span><br><span class="line">    <span class="keyword">return</span> ret;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * @brief  sdrv_demo_exit</span></span><br><span class="line"><span class="comment"> * @note   注销驱动以及驱动属性文件</span></span><br><span class="line"><span class="comment"> * @param [in]</span></span><br><span class="line"><span class="comment"> * @param [out]</span></span><br><span class="line"><span class="comment"> * @retval </span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="type">static</span> __exit <span class="type">void</span> <span class="title function_">sdrv_demo_exit</span><span class="params">(<span class="type">void</span>)</span></span><br><span class="line">&#123;</span><br><span class="line">    driver_remove_file(&amp;sdrv, &amp;driver_attr_sdrv_attr_data);</span><br><span class="line">    driver_remove_file(&amp;sdrv, &amp;sdrv_attr_name_var);</span><br><span class="line">	driver_unregister(&amp;sdrv);</span><br><span class="line"></span><br><span class="line">    PRT(<span class="string">&quot;sdrv_demo module exit!\n&quot;</span>);</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">module_init(sdrv_demo_init); <span class="comment">// 将__init定义的函数指定为驱动的入口函数</span></span><br><span class="line">module_exit(sdrv_demo_exit); <span class="comment">// 将__exit定义的函数指定为驱动的出口函数</span></span><br><span class="line"></span><br><span class="line"><span class="comment">/* 模块信息(通过 modinfo chrdev_led_demo 查看) */</span></span><br><span class="line">MODULE_LICENSE(<span class="string">&quot;GPL v2&quot;</span>);            <span class="comment">/* 源码的许可证协议 */</span></span><br><span class="line">MODULE_AUTHOR(<span class="string">&quot;sumu&quot;</span>);               <span class="comment">/* 字符串常量内容为模块作者说明 */</span></span><br><span class="line">MODULE_DESCRIPTION(<span class="string">&quot;Description&quot;</span>);   <span class="comment">/* 字符串常量内容为模块功能说明 */</span></span><br><span class="line">MODULE_ALIAS(<span class="string">&quot;module&#x27;s other name&quot;</span>); <span class="comment">/* 字符串常量内容为模块别名 */</span></span><br><span class="line"></span><br></pre></td></tr></table></figure>
              </div>
            </details>

<h2 id="4-开发板测试"><a href="#4-开发板测试" class="headerlink" title="4. 开发板测试"></a><font size=3>4. 开发板测试</font></h2><ul>
<li>（1）加载总线模块</li>
</ul>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">insmod sbus_demo.ko</span><br></pre></td></tr></table></figure>

<img data-src="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/02IMX6ULL%E5%B9%B3%E5%8F%B0/LV06-%E9%A9%B1%E5%8A%A8%E5%BC%80%E5%8F%91/LV06-04-linux%E8%AE%BE%E5%A4%87%E6%A8%A1%E5%9E%8B-09-%E8%AE%BE%E5%A4%87%E4%B8%8E%E9%A9%B1%E5%8A%A8%E7%9A%84%E5%8C%B9%E9%85%8D/img/image-20250113155339512.png" alt="image-20250113155339512"  />

<ul>
<li>（2）加载驱动模块</li>
</ul>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">insmod sdriver_demo.ko</span><br></pre></td></tr></table></figure>

<img data-src="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/02IMX6ULL%E5%B9%B3%E5%8F%B0/LV06-%E9%A9%B1%E5%8A%A8%E5%BC%80%E5%8F%91/LV06-04-linux%E8%AE%BE%E5%A4%87%E6%A8%A1%E5%9E%8B-09-%E8%AE%BE%E5%A4%87%E4%B8%8E%E9%A9%B1%E5%8A%A8%E7%9A%84%E5%8C%B9%E9%85%8D/img/image-20250113155426552.png" alt="image-20250113155426552"  />

<ul>
<li>（3）加载设备模块</li>
</ul>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">insmod sdevice_demo.ko</span><br></pre></td></tr></table></figure>

<img data-src="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/02IMX6ULL%E5%B9%B3%E5%8F%B0/LV06-%E9%A9%B1%E5%8A%A8%E5%BC%80%E5%8F%91/LV06-04-linux%E8%AE%BE%E5%A4%87%E6%A8%A1%E5%9E%8B-09-%E8%AE%BE%E5%A4%87%E4%B8%8E%E9%A9%B1%E5%8A%A8%E7%9A%84%E5%8C%B9%E9%85%8D/img/image-20250113155534905.png" alt="image-20250113155534905"  />

<p>可以看到，打印出了匹配成功的关键字。</p>
<ul>
<li>（4）查看 &#x2F;sys&#x2F;bus&#x2F;bus-name 下的文件情况</li>
</ul>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">tree /sys/bus/sbus/</span><br></pre></td></tr></table></figure>

<img data-src="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/02IMX6ULL%E5%B9%B3%E5%8F%B0/LV06-%E9%A9%B1%E5%8A%A8%E5%BC%80%E5%8F%91/LV06-04-linux%E8%AE%BE%E5%A4%87%E6%A8%A1%E5%9E%8B-09-%E8%AE%BE%E5%A4%87%E4%B8%8E%E9%A9%B1%E5%8A%A8%E7%9A%84%E5%8C%B9%E9%85%8D/img/image-20250113155704752.png" alt="image-20250113155704752"  />

<ul>
<li>（5）卸载模块</li>
</ul>
<p>需要注意，卸载模块的时候要先卸载设备和驱动的，总线的最后卸载，因为设备和驱动都依赖于总线：</p>
<img data-src="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/02IMX6ULL%E5%B9%B3%E5%8F%B0/LV06-%E9%A9%B1%E5%8A%A8%E5%BC%80%E5%8F%91/LV06-04-linux%E8%AE%BE%E5%A4%87%E6%A8%A1%E5%9E%8B-09-%E8%AE%BE%E5%A4%87%E4%B8%8E%E9%A9%B1%E5%8A%A8%E7%9A%84%E5%8C%B9%E9%85%8D/img/image-20250113155843741.png" alt="image-20250113155843741"  />

<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">rmmod sdevice_demo.ko</span><br><span class="line">rmmod sdriver_demo.ko</span><br><span class="line">rmmod sbus_demo.ko</span><br></pre></td></tr></table></figure>

<p>会有如下打印：</p>
<img data-src="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/02IMX6ULL%E5%B9%B3%E5%8F%B0/LV06-%E9%A9%B1%E5%8A%A8%E5%BC%80%E5%8F%91/LV06-04-linux%E8%AE%BE%E5%A4%87%E6%A8%A1%E5%9E%8B-09-%E8%AE%BE%E5%A4%87%E4%B8%8E%E9%A9%B1%E5%8A%A8%E7%9A%84%E5%8C%B9%E9%85%8D/img/image-20250113160006054.png" alt="image-20250113160006054"  />

<h1 id="二、xxx-probe-函数执行流程"><a href="#二、xxx-probe-函数执行流程" class="headerlink" title="二、xxx_probe()函数执行流程"></a><font size=3>二、xxx_probe()函数执行流程</font></h1><h2 id="1-driver-attach"><a href="#1-driver-attach" class="headerlink" title="1. driver_attach()"></a><font size=3>1. <a target="_blank" rel="noopener" href="https://elixir.bootlin.com/linux/v4.19.71/source/drivers/base/dd.c#L912">driver_attach()</a></font></h2><p>前面分析  <a target="_blank" rel="noopener" href="https://elixir.bootlin.com/linux/v4.19.71/source/drivers/base/bus.c#L635">bus_add_driver()</a> 函数的时候，这个函数调用了 <a target="_blank" rel="noopener" href="https://elixir.bootlin.com/linux/v4.19.71/source/drivers/base/dd.c#L912">driver_attach()</a> 函数来探测设备，我们来看一下这个  <a target="_blank" rel="noopener" href="https://elixir.bootlin.com/linux/v4.19.71/source/drivers/base/dd.c#L912">driver_attach()</a> 函数。该函数定义如下：</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="type">int</span> <span class="title function_">driver_attach</span><span class="params">(<span class="keyword">struct</span> device_driver *drv)</span></span><br><span class="line">&#123;</span><br><span class="line">	<span class="keyword">return</span> bus_for_each_dev(drv-&gt;bus, <span class="literal">NULL</span>, drv, __driver_attach);</span><br><span class="line">&#125;</span><br><span class="line">EXPORT_SYMBOL_GPL(driver_attach);</span><br></pre></td></tr></table></figure>

<p>可以看到里面调用了 <a target="_blank" rel="noopener" href="https://elixir.bootlin.com/linux/v4.19.71/source/drivers/base/bus.c#L282">bus_for_each_dev()</a> 函数。这里要注意一下，调用这个函数的时候传入了另一个函数 <a target="_blank" rel="noopener" href="https://elixir.bootlin.com/linux/v4.19.71/source/drivers/base/dd.c#L864">__driver_attach()</a>。</p>
<h3 id="1-1-bus-for-each-dev"><a href="#1-1-bus-for-each-dev" class="headerlink" title="1.1  bus_for_each_dev()"></a><font size=3>1.1  <a target="_blank" rel="noopener" href="https://elixir.bootlin.com/linux/v4.19.71/source/drivers/base/bus.c#L282">bus_for_each_dev()</a></font></h3><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line"><span class="type">int</span> <span class="title function_">bus_for_each_dev</span><span class="params">(<span class="keyword">struct</span> bus_type *bus, <span class="keyword">struct</span> device *start,</span></span><br><span class="line"><span class="params">		     <span class="type">void</span> *data, <span class="type">int</span> (*fn)(<span class="keyword">struct</span> device *, <span class="type">void</span> *))</span></span><br><span class="line">&#123;</span><br><span class="line">	<span class="class"><span class="keyword">struct</span> <span class="title">klist_iter</span> <span class="title">i</span>;</span></span><br><span class="line">	<span class="class"><span class="keyword">struct</span> <span class="title">device</span> *<span class="title">dev</span>;</span></span><br><span class="line">	<span class="type">int</span> error = <span class="number">0</span>;</span><br><span class="line">	<span class="comment">// 检查总线对象是否存在</span></span><br><span class="line">	<span class="keyword">if</span> (!bus || !bus-&gt;p)</span><br><span class="line">		<span class="keyword">return</span> -EINVAL;</span><br><span class="line">	<span class="comment">// 初始化设备列表迭代器</span></span><br><span class="line">	klist_iter_init_node(&amp;bus-&gt;p-&gt;klist_devices, &amp;i,</span><br><span class="line">			     (start ? &amp;start-&gt;p-&gt;knode_bus : <span class="literal">NULL</span>));</span><br><span class="line">    <span class="comment">// 遍历设备列表并执行指定的函数</span></span><br><span class="line">	<span class="keyword">while</span> (!error &amp;&amp; (dev = next_device(&amp;i)))</span><br><span class="line">		error = fn(dev, data);</span><br><span class="line">    <span class="comment">// 退出设备列表迭代器</span></span><br><span class="line">	klist_iter_exit(&amp;i);</span><br><span class="line">	<span class="keyword">return</span> error;</span><br><span class="line">&#125;</span><br><span class="line">EXPORT_SYMBOL_GPL(bus_for_each_dev);</span><br></pre></td></tr></table></figure>

<p>这个函数的作用是遍历指定总线上的所有设备，并对每个设备执行指定的函数 fn。</p>
<blockquote>
<p>这个函数的参数说明如下：</p>
<p>bus：指定要遍历的总线对象。 </p>
<p>start：指定开始遍历的设备对象。如果为 NULL，则从总线的第一个设备开始遍历。 </p>
<p>data：传递给函数 fn 的额外数据。 </p>
<p>fn：指定要执行的函数，该函数接受一个设备对象和额外数据作为参数，并返回一个整数错误码。在这里，这个指针指向 <a target="_blank" rel="noopener" href="https://elixir.bootlin.com/linux/v4.19.71/source/drivers/base/dd.c#L864">__driver_attach()</a> 函数</p>
</blockquote>
<ul>
<li>第 <a target="_blank" rel="noopener" href="https://elixir.bootlin.com/linux/v4.19.71/source/drivers/base/bus.c#L308">308</a> - <a target="_blank" rel="noopener" href="https://elixir.bootlin.com/linux/v4.19.71/source/drivers/base/bus.c#L309">309</a> 行</li>
</ul>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">if</span> (!bus || !bus-&gt;p)</span><br><span class="line">	<span class="keyword">return</span> -EINVAL;</span><br></pre></td></tr></table></figure>

<p>首先，检查传入的总线对象是否存在以及与该总线相关的私有数据是否存 在。如果总线对象或其私有数据不存在，返回 -EINVAL 表示无效的参数错误码。 </p>
<ul>
<li>第 <a target="_blank" rel="noopener" href="https://elixir.bootlin.com/linux/v4.19.71/source/drivers/base/bus.c#L311">311</a> - <a target="_blank" rel="noopener" href="https://elixir.bootlin.com/linux/v4.19.71/source/drivers/base/bus.c#L312">312</a> 行</li>
</ul>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">klist_iter_init_node(&amp;bus-&gt;p-&gt;klist_devices, &amp;i,</span><br><span class="line">		     (start ? &amp;start-&gt;p-&gt;knode_bus : <span class="literal">NULL</span>));</span><br></pre></td></tr></table></figure>

<p>接下来，初始化设备列表迭代器，以便遍历总线上的设备。 使用 <a target="_blank" rel="noopener" href="https://elixir.bootlin.com/linux/v4.19.71/source/lib/klist.c#L272">klist_iter_init_node()</a> 函数初始化一个设备列表迭代器。传递总线对象的设备列表 klist_devices、迭代器对象 i，以及可选的起始设备的节点指针。然后，在一个循环中遍历设备列表并执行指定的函数。 </p>
<ul>
<li>第 <a target="_blank" rel="noopener" href="https://elixir.bootlin.com/linux/v4.19.71/source/drivers/base/bus.c#L313">313</a> - <a target="_blank" rel="noopener" href="https://elixir.bootlin.com/linux/v4.19.71/source/drivers/base/bus.c#L314">314</a> 行</li>
</ul>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">while</span> (!error &amp;&amp; (dev = next_device(&amp;i)))</span><br><span class="line">	error = fn(dev, data);</span><br></pre></td></tr></table></figure>

<p>使用 <a target="_blank" rel="noopener" href="https://elixir.bootlin.com/linux/v4.19.71/source/drivers/base/bus.c#L269">next_device()</a> 函数从迭代器中获取下一个设备。如果存在下一个设 备，则调用传入的函数指针 fn，并将当前设备和额外的数据参数传递给它。如果执行函数时出现错误，将错误码赋值给 error。 </p>
<ul>
<li>第 <a target="_blank" rel="noopener" href="https://elixir.bootlin.com/linux/v4.19.71/source/drivers/base/bus.c#L315">315</a> 行</li>
</ul>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">klist_iter_exit(&amp;i);</span><br></pre></td></tr></table></figure>

<p>最后，退出设备列表迭代器，释放相关资源。使用 <a target="_blank" rel="noopener" href="https://elixir.bootlin.com/linux/v4.19.71/source/lib/klist.c#L304">klist_iter_exit()</a>函数退 出设备列表的迭代器。 总的来说， <a target="_blank" rel="noopener" href="https://elixir.bootlin.com/linux/v4.19.71/source/drivers/base/bus.c#L282">bus_for_each_dev()</a> 函数主要是提供了一个遍历指定总线上的设备对象列表， 并对每个设备对象进行特定操作的快捷方式，可以用于驱动程序中需要管理和操作大量设备实例的场景。</p>
<h3 id="1-2-driver-attach"><a href="#1-2-driver-attach" class="headerlink" title="1.2 __driver_attach() "></a><font size=3>1.2 <a target="_blank" rel="noopener" href="https://elixir.bootlin.com/linux/v4.19.71/source/drivers/base/dd.c#L864">__driver_attach()</a> </font></h3><p>前面知道fn指针指向的是<a target="_blank" rel="noopener" href="https://elixir.bootlin.com/linux/v4.19.71/source/drivers/base/dd.c#L864">__driver_attach()</a> 函数：</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br></pre></td><td class="code"><pre><span class="line"><span class="type">static</span> <span class="type">int</span> __driver_attach(<span class="keyword">struct</span> device *dev, <span class="type">void</span> *data)</span><br><span class="line">&#123;</span><br><span class="line">    <span class="comment">// 传入的数据参数作为设备驱动对象</span></span><br><span class="line">	<span class="class"><span class="keyword">struct</span> <span class="title">device_driver</span> *<span class="title">drv</span> =</span> data;</span><br><span class="line">	<span class="type">int</span> ret;</span><br><span class="line"></span><br><span class="line">	<span class="comment">/*</span></span><br><span class="line"><span class="comment">	 * Lock device and try to bind to it. We drop the error</span></span><br><span class="line"><span class="comment">	 * here and always return 0, because we need to keep trying</span></span><br><span class="line"><span class="comment">	 * to bind to devices and some drivers will return an error</span></span><br><span class="line"><span class="comment">	 * simply if it didn&#x27;t support the device.</span></span><br><span class="line"><span class="comment">	 *</span></span><br><span class="line"><span class="comment">	 * driver_probe_device() will spit a warning if there</span></span><br><span class="line"><span class="comment">	 * is an error.</span></span><br><span class="line"><span class="comment">	 */</span></span><br><span class="line"></span><br><span class="line">	ret = driver_match_device(drv, dev);<span class="comment">// 尝试将驱动程序绑定到设备上</span></span><br><span class="line">	<span class="keyword">if</span> (ret == <span class="number">0</span>) &#123;</span><br><span class="line">		<span class="comment">/* no match */</span></span><br><span class="line">		<span class="keyword">return</span> <span class="number">0</span>;<span class="comment">// 如果没有匹配，则返回 0</span></span><br><span class="line">	&#125; <span class="keyword">else</span> <span class="keyword">if</span> (ret == -EPROBE_DEFER) &#123;</span><br><span class="line">		dev_dbg(dev, <span class="string">&quot;Device match requests probe deferral\n&quot;</span>);</span><br><span class="line">		driver_deferred_probe_add(dev);<span class="comment">// 请求推迟探测设备</span></span><br><span class="line">	&#125; <span class="keyword">else</span> <span class="keyword">if</span> (ret &lt; <span class="number">0</span>) &#123;</span><br><span class="line">		dev_dbg(dev, <span class="string">&quot;Bus failed to match device: %d&quot;</span>, ret);</span><br><span class="line">		<span class="keyword">return</span> ret;<span class="comment">// 总线无法匹配设备，返回错误码</span></span><br><span class="line">	&#125; <span class="comment">/* ret &gt; 0 means positive match */</span></span><br><span class="line"></span><br><span class="line">	<span class="keyword">if</span> (dev-&gt;parent &amp;&amp; dev-&gt;bus-&gt;need_parent_lock)</span><br><span class="line">		device_lock(dev-&gt;parent);</span><br><span class="line">	device_lock(dev);<span class="comment">// 锁定设备以保护 dev-&gt;driver 和 async_driver 字段</span></span><br><span class="line">	<span class="keyword">if</span> (!dev-&gt;p-&gt;dead &amp;&amp; !dev-&gt;driver)</span><br><span class="line">		driver_probe_device(drv, dev);<span class="comment">//最终这里会执行到bus的xxx_probe()函数</span></span><br><span class="line">	device_unlock(dev);</span><br><span class="line">	<span class="keyword">if</span> (dev-&gt;parent &amp;&amp; dev-&gt;bus-&gt;need_parent_lock)</span><br><span class="line">		device_unlock(dev-&gt;parent);</span><br><span class="line"></span><br><span class="line">	<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p><a target="_blank" rel="noopener" href="https://elixir.bootlin.com/linux/v4.19.71/source/drivers/base/dd.c#L864">__driver_attach()</a> 中使用 <a target="_blank" rel="noopener" href="https://elixir.bootlin.com/linux/v4.19.71/source/drivers/base/base.h#L120">driver_match_device()</a>函数尝试将驱动程序绑定到设备上。可以看到，函数返回大于0的值后表示匹配成功。继续往下会调用 <a target="_blank" rel="noopener" href="https://elixir.bootlin.com/linux/v4.19.71/source/drivers/base/dd.c#L630">driver_probe_device()</a>  函数来尝试绑定设备和驱动程序。其他的我们暂时不关心。</p>
<h4 id="1-2-1-driver-match-device"><a href="#1-2-1-driver-match-device" class="headerlink" title=" 1.2.1 driver_match_device()"></a><font size=3> 1.2.1 <a target="_blank" rel="noopener" href="https://elixir.bootlin.com/linux/v4.19.71/source/drivers/base/base.h#L120">driver_match_device()</a></font></h4><p>我们来看一下 <a target="_blank" rel="noopener" href="https://elixir.bootlin.com/linux/v4.19.71/source/drivers/base/base.h#L120">driver_match_device()</a> 函数：</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="type">static</span> <span class="keyword">inline</span> <span class="type">int</span> <span class="title function_">driver_match_device</span><span class="params">(<span class="keyword">struct</span> device_driver *drv,</span></span><br><span class="line"><span class="params">				      <span class="keyword">struct</span> device *dev)</span></span><br><span class="line">&#123;</span><br><span class="line">	<span class="keyword">return</span> drv-&gt;bus-&gt;match ? drv-&gt;bus-&gt;match(dev, drv) : <span class="number">1</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>可以看到，该函数用于检查设备是否与驱动程序匹配。其中，drv是指向设备驱动程序对象的指针，dev是指向设备对象的指针。它的执行过程如下：</p>
<p>（1）检查驱动程序对象的 bus 字段是否为 NULL，以及 bus 字段的 match 函数是否存在。 驱动程序对象的 bus 字段表示该驱动程序所属的总线。match 函数是总线对象中的一个函数指 针，用于检查设备与驱动程序是否匹配。</p>
<p>（2）如果 match函数存在，则调用总线对象的 match 函数，传入设备对象和驱动程序对象作为参数。drv-&gt;bus-&gt;match(dev, drv)表示调用总线对象的 match 函数，并将设备对象和驱动程序对象 作为参数传递给该函数。dev 是用于匹配的设备对象。drv 是用于匹配的驱动程序对象。</p>
<p>（3）如果总线对象的 match 函数返回 0，则表示设备与驱动程序不匹配，函数将返回 0。返回值为 0 表示不匹配。</p>
<p>（4）如果总线对象的 match 函数返回非零值（大于 0），则表示设备与驱动程序匹配，函数将返回 1。返回值为 1 表示匹配。</p>
<p>（5）如果总线对象的 match 函数不存在（为 NULL），则默认认为设备与驱动程序匹配，函数将返回 1。</p>
<p>总之，它其实就是调用了 drv-&gt;bus-&gt;match 函数，就是我们前面总线中实现的xxx_match()函数来完成匹配。这个逻辑就是先判断一下match函数是否为空，若为空，就返回1，上一级函数 <a target="_blank" rel="noopener" href="https://elixir.bootlin.com/linux/v4.19.71/source/drivers/base/dd.c#L864">__driver_attach()</a> 就可以正常往下执行，若是match函数不为空，这个时候返回值就是函数执行的结果，当返回0的时候表示没有设备匹配，返回1表示有设备匹配成功。</p>
<h4 id="1-2-2-driver-probe-device"><a href="#1-2-2-driver-probe-device" class="headerlink" title="1.2.2 driver_probe_device()"></a><font size=3>1.2.2 <a target="_blank" rel="noopener" href="https://elixir.bootlin.com/linux/v4.19.71/source/drivers/base/dd.c#L630">driver_probe_device()</a></font></h4><p>如果设备和驱动匹配上，会继续执行 <a target="_blank" rel="noopener" href="https://elixir.bootlin.com/linux/v4.19.71/source/drivers/base/dd.c#L630">driver_probe_device()</a>函数，它的定义如下：</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br></pre></td><td class="code"><pre><span class="line"><span class="type">int</span> <span class="title function_">driver_probe_device</span><span class="params">(<span class="keyword">struct</span> device_driver *drv, <span class="keyword">struct</span> device *dev)</span></span><br><span class="line">&#123;</span><br><span class="line">	<span class="type">int</span> ret = <span class="number">0</span>;</span><br><span class="line">	<span class="comment">// 检查设备是否已注册，如果未注册则返回错误码 -ENODEV</span></span><br><span class="line">	<span class="keyword">if</span> (!device_is_registered(dev))</span><br><span class="line">		<span class="keyword">return</span> -ENODEV;</span><br><span class="line">	<span class="comment">// 打印调试信息，表示设备与驱动程序匹配</span></span><br><span class="line">	pr_debug(<span class="string">&quot;bus: &#x27;%s&#x27;: %s: matched device %s with driver %s\n&quot;</span>,</span><br><span class="line">		 drv-&gt;bus-&gt;name, __func__, dev_name(dev), drv-&gt;name);</span><br><span class="line">	<span class="comment">// 获取设备供应商的运行时引用计数</span></span><br><span class="line">	pm_runtime_get_suppliers(dev);</span><br><span class="line">    <span class="comment">// 如果设备有父设备，获取父设备的同步运行时引用计数</span></span><br><span class="line">	<span class="keyword">if</span> (dev-&gt;parent)</span><br><span class="line">		pm_runtime_get_sync(dev-&gt;parent);</span><br><span class="line">	<span class="comment">// 等待设备的运行时状态达到稳定</span></span><br><span class="line">	pm_runtime_barrier(dev);</span><br><span class="line">    <span class="comment">// 根据初始化调试标志选择调用真实的探测函数</span></span><br><span class="line">	<span class="keyword">if</span> (initcall_debug)</span><br><span class="line">		ret = really_probe_debug(dev, drv);</span><br><span class="line">	<span class="keyword">else</span></span><br><span class="line">		ret = really_probe(dev, drv);</span><br><span class="line">    <span class="comment">// 请求设备进入空闲状态（省电模式）</span></span><br><span class="line">	pm_request_idle(dev);</span><br><span class="line">	<span class="comment">// 如果设备有父设备，释放父设备的运行时引用计数</span></span><br><span class="line">	<span class="keyword">if</span> (dev-&gt;parent)</span><br><span class="line">		pm_runtime_put(dev-&gt;parent);</span><br><span class="line">	<span class="comment">// 释放设备供应商的运行时引用计数</span></span><br><span class="line">	pm_runtime_put_suppliers(dev);</span><br><span class="line">    <span class="comment">// 返回探测函数的执行结果</span></span><br><span class="line">	<span class="keyword">return</span> ret;</span><br><span class="line">&#125;</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<p>其他先不管，我们直接看第 <a target="_blank" rel="noopener" href="https://elixir.bootlin.com/linux/v4.19.71/source/drivers/base/dd.c#L658">658</a> - <a target="_blank" rel="noopener" href="https://elixir.bootlin.com/linux/v4.19.71/source/drivers/base/dd.c#L661">661</a> 行，上面这个 initcall_debug 是个全局变量，应该是调试的时候用，调试的时候调用 <a target="_blank" rel="noopener" href="https://elixir.bootlin.com/linux/v4.19.71/source/drivers/base/dd.c#L586">really_probe_debug()</a>  函数，这个函数内部也是在调用 <a target="_blank" rel="noopener" href="https://elixir.bootlin.com/linux/v4.19.71/source/drivers/base/dd.c#L446">really_probe()</a> 函数，我们直接看这个  <a target="_blank" rel="noopener" href="https://elixir.bootlin.com/linux/v4.19.71/source/drivers/base/dd.c#L446">really_probe()</a> 函数：</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="type">static</span> <span class="type">int</span> <span class="title function_">really_probe</span><span class="params">(<span class="keyword">struct</span> device *dev, <span class="keyword">struct</span> device_driver *drv)</span></span><br><span class="line">&#123;</span><br><span class="line">	<span class="comment">// ......</span></span><br><span class="line">	<span class="keyword">if</span> (dev-&gt;bus-&gt;probe) &#123;</span><br><span class="line">		ret = dev-&gt;bus-&gt;probe(dev);</span><br><span class="line">		<span class="keyword">if</span> (ret)</span><br><span class="line">			<span class="keyword">goto</span> probe_failed;</span><br><span class="line">	&#125; <span class="keyword">else</span> <span class="keyword">if</span> (drv-&gt;probe) &#123;</span><br><span class="line">		ret = drv-&gt;probe(dev);</span><br><span class="line">		<span class="keyword">if</span> (ret)</span><br><span class="line">			<span class="keyword">goto</span> probe_failed;</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="comment">// ......</span></span><br><span class="line">	<span class="keyword">return</span> ret;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>这个函数挺长的，别的就没咋关心了，直接看第 <a target="_blank" rel="noopener" href="https://elixir.bootlin.com/linux/v4.19.71/source/drivers/base/dd.c#L499">499</a> - <a target="_blank" rel="noopener" href="https://elixir.bootlin.com/linux/v4.19.71/source/drivers/base/dd.c#L507">507</a> 行这段。从这里可以看到，这里其实就是判断了一下dev-&gt;bus-&gt;probe、drv-&gt;probe这两个函数是否存在，存在的话就执行。 dev-&gt;bus-&gt;probe这个函数其实就是总线中实现的xxx_probe()函数，而 drv-&gt;probe 则是驱动中实现的xxx_probe()函数，上面的逻辑就是优先执行总线中的probe函数，若是总线中不存在，我们还可以找驱动中的probe函数执行。</p>
<h3 id="1-3-总结"><a href="#1-3-总结" class="headerlink" title="1.3 总结"></a><font size=3>1.3 总结</font></h3><p>经过前面代码的分析，设备和驱动匹配流程函数的调用关系如下图所示：</p>
<img data-src="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/02IMX6ULL%E5%B9%B3%E5%8F%B0/LV06-%E9%A9%B1%E5%8A%A8%E5%BC%80%E5%8F%91/LV06-04-linux%E8%AE%BE%E5%A4%87%E6%A8%A1%E5%9E%8B-09-%E8%AE%BE%E5%A4%87%E4%B8%8E%E9%A9%B1%E5%8A%A8%E7%9A%84%E5%8C%B9%E9%85%8D/img/image-20250113193242318.png" alt="image-20250113193242318"  />

<h2 id="2-really-probe"><a href="#2-really-probe" class="headerlink" title="2. really_probe()"></a><font size=3>2. <a target="_blank" rel="noopener" href="https://elixir.bootlin.com/linux/v4.19.71/source/drivers/base/dd.c#L446">really_probe()</a></font></h2><p><a target="_blank" rel="noopener" href="https://elixir.bootlin.com/linux/v4.19.71/source/drivers/base/dd.c#L446">really_probe()</a> 函数定义如下：</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span class="line">126</span><br><span class="line">127</span><br><span class="line">128</span><br><span class="line">129</span><br><span class="line">130</span><br><span class="line">131</span><br><span class="line">132</span><br><span class="line">133</span><br><span class="line">134</span><br><span class="line">135</span><br><span class="line">136</span><br><span class="line">137</span><br><span class="line">138</span><br><span class="line">139</span><br><span class="line">140</span><br><span class="line">141</span><br><span class="line">142</span><br><span class="line">143</span><br><span class="line">144</span><br><span class="line">145</span><br><span class="line">146</span><br><span class="line">147</span><br></pre></td><td class="code"><pre><span class="line"><span class="type">static</span> <span class="type">int</span> <span class="title function_">really_probe</span><span class="params">(<span class="keyword">struct</span> device *dev, <span class="keyword">struct</span> device_driver *drv)</span></span><br><span class="line">&#123;</span><br><span class="line">	<span class="type">int</span> ret = -EPROBE_DEFER; <span class="comment">// 初始化返回值为延迟探测</span></span><br><span class="line">    <span class="comment">// 获取当前延迟探测计数</span></span><br><span class="line">	<span class="type">int</span> local_trigger_count = <span class="type">atomic_read</span>(&amp;deferred_trigger_count);</span><br><span class="line">    <span class="comment">// 判断是否启用了驱动移除测试</span></span><br><span class="line">	<span class="type">bool</span> test_remove = IS_ENABLED(CONFIG_DEBUG_TEST_DRIVER_REMOVE) &amp;&amp;</span><br><span class="line">			   !drv-&gt;suppress_bind_attrs;</span><br><span class="line"></span><br><span class="line">	<span class="keyword">if</span> (defer_all_probes) &#123;</span><br><span class="line">		<span class="comment">/*</span></span><br><span class="line"><span class="comment">		 * Value of defer_all_probes can be set only by</span></span><br><span class="line"><span class="comment">		 * device_defer_all_probes_enable() which, in turn, will call</span></span><br><span class="line"><span class="comment">		 * wait_for_device_probe() right after that to avoid any races.</span></span><br><span class="line"><span class="comment">		 */</span></span><br><span class="line">        <span class="comment">/* defer_all_probes 的值只能通过 device_defer_all_probes_enable() 设置，</span></span><br><span class="line"><span class="comment">         * 而该函数会紧接着调用 wait_for_device_probe()，以避免任何竞争情况。</span></span><br><span class="line"><span class="comment">         */</span></span><br><span class="line">		dev_dbg(dev, <span class="string">&quot;Driver %s force probe deferral\n&quot;</span>, drv-&gt;name);</span><br><span class="line">		driver_deferred_probe_add(dev);</span><br><span class="line">		<span class="keyword">return</span> ret;</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="comment">// 检查设备的供应者链路</span></span><br><span class="line">	ret = device_links_check_suppliers(dev);</span><br><span class="line">	<span class="keyword">if</span> (ret == -EPROBE_DEFER)</span><br><span class="line">		driver_deferred_probe_add_trigger(dev, local_trigger_count);<span class="comment">// 将设备添加到延迟探测触发列表</span></span><br><span class="line">	<span class="keyword">if</span> (ret)</span><br><span class="line">		<span class="keyword">return</span> ret;</span><br><span class="line">	<span class="comment">// 增加探测计数</span></span><br><span class="line">	<span class="type">atomic_inc</span>(&amp;probe_count);</span><br><span class="line">	pr_debug(<span class="string">&quot;bus: &#x27;%s&#x27;: %s: probing driver %s with device %s\n&quot;</span>,</span><br><span class="line">		 drv-&gt;bus-&gt;name, __func__, drv-&gt;name, dev_name(dev));</span><br><span class="line">	WARN_ON(!list_empty(&amp;dev-&gt;devres_head));</span><br><span class="line"></span><br><span class="line">re_probe:</span><br><span class="line">	dev-&gt;driver = drv;</span><br><span class="line"></span><br><span class="line">	<span class="comment">/* If using pinctrl, bind pins now before probing */</span></span><br><span class="line">    <span class="comment">/* 如果使用了 pinctrl，绑定引脚 */</span></span><br><span class="line">	ret = pinctrl_bind_pins(dev);</span><br><span class="line">	<span class="keyword">if</span> (ret)</span><br><span class="line">		<span class="keyword">goto</span> pinctrl_bind_failed;</span><br><span class="line">	<span class="comment">// 配置 DMA</span></span><br><span class="line">	ret = dma_configure(dev);</span><br><span class="line">	<span class="keyword">if</span> (ret)</span><br><span class="line">		<span class="keyword">goto</span> probe_failed;</span><br><span class="line">	<span class="comment">// 添加驱动的 sysfs</span></span><br><span class="line">	<span class="keyword">if</span> (driver_sysfs_add(dev)) &#123;</span><br><span class="line">		printk(KERN_ERR <span class="string">&quot;%s: driver_sysfs_add(%s) failed\n&quot;</span>,</span><br><span class="line">			__func__, dev_name(dev));</span><br><span class="line">		<span class="keyword">goto</span> probe_failed;</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="comment">// 如果设备有电源管理域并且存在激活函数，激活电源管理域</span></span><br><span class="line">	<span class="keyword">if</span> (dev-&gt;pm_domain &amp;&amp; dev-&gt;pm_domain-&gt;activate) &#123;</span><br><span class="line">		ret = dev-&gt;pm_domain-&gt;activate(dev);</span><br><span class="line">		<span class="keyword">if</span> (ret)</span><br><span class="line">			<span class="keyword">goto</span> probe_failed;</span><br><span class="line">	&#125;</span><br><span class="line">	</span><br><span class="line">	<span class="keyword">if</span> (dev-&gt;bus-&gt;probe) &#123; <span class="comment">// 如果总线有探测函数，调用总线的探测函数</span></span><br><span class="line">		ret = dev-&gt;bus-&gt;probe(dev);</span><br><span class="line">		<span class="keyword">if</span> (ret)</span><br><span class="line">			<span class="keyword">goto</span> probe_failed;</span><br><span class="line">	&#125; <span class="keyword">else</span> <span class="keyword">if</span> (drv-&gt;probe) &#123; <span class="comment">// 否则调用驱动的探测函数</span></span><br><span class="line">		ret = drv-&gt;probe(dev);</span><br><span class="line">		<span class="keyword">if</span> (ret)</span><br><span class="line">			<span class="keyword">goto</span> probe_failed;</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="comment">// 如果启用了驱动移除测试</span></span><br><span class="line">	<span class="keyword">if</span> (test_remove) &#123;</span><br><span class="line">		test_remove = <span class="literal">false</span>;</span><br><span class="line">		</span><br><span class="line">		<span class="keyword">if</span> (dev-&gt;bus-&gt;remove) <span class="comment">// 如果总线有移除函数，调用总线的移除函数</span></span><br><span class="line">			dev-&gt;bus-&gt;remove(dev);</span><br><span class="line">		<span class="keyword">else</span> <span class="keyword">if</span> (drv-&gt;remove) <span class="comment">// 否则调用驱动的移除函数</span></span><br><span class="line">			drv-&gt;remove(dev);</span><br><span class="line"></span><br><span class="line">		devres_release_all(dev); <span class="comment">// 释放设备的资源</span></span><br><span class="line">		driver_sysfs_remove(dev);<span class="comment">// 移除驱动的 sysfs</span></span><br><span class="line">		dev-&gt;driver = <span class="literal">NULL</span>;</span><br><span class="line">		dev_set_drvdata(dev, <span class="literal">NULL</span>);</span><br><span class="line">        <span class="comment">// 如果设备有电源管理域并且存在解除函数，解除电源管理域</span></span><br><span class="line">		<span class="keyword">if</span> (dev-&gt;pm_domain &amp;&amp; dev-&gt;pm_domain-&gt;dismiss)</span><br><span class="line">			dev-&gt;pm_domain-&gt;dismiss(dev);</span><br><span class="line">		pm_runtime_reinit(dev);<span class="comment">// 重新初始化给定设备对象中的运行时PM字段。</span></span><br><span class="line"></span><br><span class="line">		<span class="keyword">goto</span> re_probe;<span class="comment">// 重新进行探测</span></span><br><span class="line">	&#125;</span><br><span class="line">	<span class="comment">// 完成 pinctrl 的初始化</span></span><br><span class="line">	pinctrl_init_done(dev);</span><br><span class="line">	<span class="comment">// 如果设备有电源管理域并且存在同步函数，同步电源管理域</span></span><br><span class="line">	<span class="keyword">if</span> (dev-&gt;pm_domain &amp;&amp; dev-&gt;pm_domain-&gt;sync)</span><br><span class="line">		dev-&gt;pm_domain-&gt;sync(dev);</span><br><span class="line">	<span class="comment">// 驱动绑定成功</span></span><br><span class="line">	driver_bound(dev);</span><br><span class="line">	ret = <span class="number">1</span>;</span><br><span class="line">	pr_debug(<span class="string">&quot;bus: &#x27;%s&#x27;: %s: bound device %s to driver %s\n&quot;</span>,</span><br><span class="line">		 drv-&gt;bus-&gt;name, __func__, dev_name(dev), drv-&gt;name);</span><br><span class="line">	<span class="keyword">goto</span> done;</span><br><span class="line"></span><br><span class="line">probe_failed:</span><br><span class="line">	<span class="keyword">if</span> (dev-&gt;bus)</span><br><span class="line">		blocking_notifier_call_chain(&amp;dev-&gt;bus-&gt;p-&gt;bus_notifier,</span><br><span class="line">					     BUS_NOTIFY_DRIVER_NOT_BOUND, dev);</span><br><span class="line">pinctrl_bind_failed:</span><br><span class="line">	device_links_no_driver(dev); <span class="comment">// 将设备与驱动解除绑定</span></span><br><span class="line">	devres_release_all(dev);<span class="comment">// 释放设备的资源</span></span><br><span class="line">	dma_deconfigure(dev);   <span class="comment">// 取消 DMA 配</span></span><br><span class="line">	driver_sysfs_remove(dev); <span class="comment">// 移除驱动的 sysfs</span></span><br><span class="line">	dev-&gt;driver = <span class="literal">NULL</span>;</span><br><span class="line">	dev_set_drvdata(dev, <span class="literal">NULL</span>);</span><br><span class="line">    <span class="comment">// 如果设备有电源管理域并且存在解除函数，解除电源管理域</span></span><br><span class="line">	<span class="keyword">if</span> (dev-&gt;pm_domain &amp;&amp; dev-&gt;pm_domain-&gt;dismiss)</span><br><span class="line">		dev-&gt;pm_domain-&gt;dismiss(dev);</span><br><span class="line">	pm_runtime_reinit(dev);</span><br><span class="line">	dev_pm_set_driver_flags(dev, <span class="number">0</span>);<span class="comment">// 设置设备的驱动标志为 0</span></span><br><span class="line"></span><br><span class="line">	<span class="keyword">switch</span> (ret) &#123;</span><br><span class="line">	<span class="keyword">case</span> -EPROBE_DEFER:</span><br><span class="line">		<span class="comment">/* Driver requested deferred probing */</span></span><br><span class="line">        <span class="comment">/* 驱动程序请求延迟探测 */</span></span><br><span class="line">		dev_dbg(dev, <span class="string">&quot;Driver %s requests probe deferral\n&quot;</span>, drv-&gt;name);</span><br><span class="line">        <span class="comment">// 将设备添加到延迟探测触发列表</span></span><br><span class="line">		driver_deferred_probe_add_trigger(dev, local_trigger_count);</span><br><span class="line">		<span class="keyword">break</span>;</span><br><span class="line">	<span class="keyword">case</span> -ENODEV:</span><br><span class="line">	<span class="keyword">case</span> -ENXIO:</span><br><span class="line">		pr_debug(<span class="string">&quot;%s: probe of %s rejects match %d\n&quot;</span>,</span><br><span class="line">			 drv-&gt;name, dev_name(dev), ret);</span><br><span class="line">		<span class="keyword">break</span>;</span><br><span class="line">	<span class="keyword">default</span>:</span><br><span class="line">		<span class="comment">/* driver matched but the probe failed */</span></span><br><span class="line">		printk(KERN_WARNING</span><br><span class="line">		       <span class="string">&quot;%s: probe of %s failed with error %d\n&quot;</span>,</span><br><span class="line">		       drv-&gt;name, dev_name(dev), ret);</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="comment">/*</span></span><br><span class="line"><span class="comment">	 * Ignore errors returned by -&gt;probe so that the next driver can try</span></span><br><span class="line"><span class="comment">	 * its luck.</span></span><br><span class="line"><span class="comment">	 */</span></span><br><span class="line">   <span class="comment">/* 忽略 -&gt;probe 返回的错误，以便下一个驱动程序可以尝试运行。*/</span></span><br><span class="line">	ret = <span class="number">0</span>;</span><br><span class="line">done:</span><br><span class="line">	<span class="type">atomic_dec</span>(&amp;probe_count); <span class="comment">// 减少探测计数</span></span><br><span class="line">	wake_up(&amp;probe_waitqueue);<span class="comment">// 唤醒等待探测的进程</span></span><br><span class="line">	<span class="keyword">return</span> ret;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h1 id="三、驱动和设备加载的先后顺序"><a href="#三、驱动和设备加载的先后顺序" class="headerlink" title="三、驱动和设备加载的先后顺序"></a><font size=3>三、驱动和设备加载的先后顺序</font></h1><p>由于驱动和设备都要用到总线中的符号，所以总线一定是最先加载的，这个毫无疑问，其实在测试demo的过程中，会发现不管先加载驱动，还是先加载设备，最终都会完成匹配。</p>
<p>加载驱动的时候上面已经分析过了，会在 <a target="_blank" rel="noopener" href="https://elixir.bootlin.com/linux/v4.19.71/source/drivers/base/dd.c#L864">__driver_attach()</a> 函数中调用总线中的匹配函数完成设备和驱动的匹配（若是有驱动在的话）。那么先加载驱动后加载设备，也是可以完成匹配的，设备加载的时候是怎么完成匹配的呢？</p>
<h2 id="1-device-add"><a href="#1-device-add" class="headerlink" title="1. device_add()"></a><font size=3>1. <a target="_blank" rel="noopener" href="https://elixir.bootlin.com/linux/v4.19.71/source/drivers/base/core.c#L1809">device_add()</a></font></h2><p><a target="_blank" rel="noopener" href="https://elixir.bootlin.com/linux/v4.19.71/source/drivers/base/core.c#L1809">device_add()</a>函数定义如下：</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="type">int</span> <span class="title function_">device_add</span><span class="params">(<span class="keyword">struct</span> device *dev)</span></span><br><span class="line">&#123;</span><br><span class="line">	<span class="comment">// ......</span></span><br><span class="line">	bus_probe_device(dev);</span><br><span class="line">	<span class="keyword">if</span> (parent)</span><br><span class="line">		klist_add_tail(&amp;dev-&gt;p-&gt;knode_parent,</span><br><span class="line">			       &amp;parent-&gt;p-&gt;klist_children);</span><br><span class="line">	<span class="comment">// ......</span></span><br><span class="line">&#125;</span><br><span class="line">EXPORT_SYMBOL_GPL(device_add);</span><br></pre></td></tr></table></figure>

<p>该函数内部调用了 <a target="_blank" rel="noopener" href="https://elixir.bootlin.com/linux/v4.19.71/source/drivers/base/bus.c#L509">bus_probe_device()</a> 函数，这个函数定义如下：</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line"><span class="type">void</span> <span class="title function_">bus_probe_device</span><span class="params">(<span class="keyword">struct</span> device *dev)</span></span><br><span class="line">&#123;</span><br><span class="line">	<span class="class"><span class="keyword">struct</span> <span class="title">bus_type</span> *<span class="title">bus</span> =</span> dev-&gt;bus;</span><br><span class="line">	<span class="class"><span class="keyword">struct</span> <span class="title">subsys_interface</span> *<span class="title">sif</span>;</span></span><br><span class="line"></span><br><span class="line">	<span class="keyword">if</span> (!bus)</span><br><span class="line">		<span class="keyword">return</span>;</span><br><span class="line"></span><br><span class="line">	<span class="keyword">if</span> (bus-&gt;p-&gt;drivers_autoprobe)</span><br><span class="line">		device_initial_probe(dev);</span><br><span class="line"></span><br><span class="line">	mutex_lock(&amp;bus-&gt;p-&gt;mutex);</span><br><span class="line">	list_for_each_entry(sif, &amp;bus-&gt;p-&gt;interfaces, node)</span><br><span class="line">		<span class="keyword">if</span> (sif-&gt;add_dev)</span><br><span class="line">			sif-&gt;add_dev(dev, sif);</span><br><span class="line">	mutex_unlock(&amp;bus-&gt;p-&gt;mutex);</span><br><span class="line">&#125;</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<p>可以看到在函数内部调用了 <a target="_blank" rel="noopener" href="https://elixir.bootlin.com/linux/v4.19.71/source/drivers/base/dd.c#L859">device_initial_probe()</a> 函数，该函数定义如下：</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="type">void</span> <span class="title function_">device_initial_probe</span><span class="params">(<span class="keyword">struct</span> device *dev)</span></span><br><span class="line">&#123;</span><br><span class="line">	__device_attach(dev, <span class="literal">true</span>);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>可以看到这个函数最终又调用了 <a target="_blank" rel="noopener" href="https://elixir.bootlin.com/linux/v4.19.71/source/drivers/base/dd.c#L787">__device_attach()</a> 函数。</p>
<h2 id="2-device-attach"><a href="#2-device-attach" class="headerlink" title="2. __device_attach()"></a><font size=3>2. <a target="_blank" rel="noopener" href="https://elixir.bootlin.com/linux/v4.19.71/source/drivers/base/dd.c#L787">__device_attach()</a></font></h2><p><a target="_blank" rel="noopener" href="https://elixir.bootlin.com/linux/v4.19.71/source/drivers/base/dd.c#L787">__device_attach()</a> 函数定义如下：</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br></pre></td><td class="code"><pre><span class="line"><span class="type">static</span> <span class="type">int</span> __device_attach(<span class="keyword">struct</span> device *dev, <span class="type">bool</span> allow_async)</span><br><span class="line">&#123;</span><br><span class="line">	<span class="type">int</span> ret = <span class="number">0</span>;</span><br><span class="line"></span><br><span class="line">	device_lock(dev);</span><br><span class="line">	<span class="keyword">if</span> (dev-&gt;driver) &#123;</span><br><span class="line">		<span class="keyword">if</span> (device_is_bound(dev)) &#123; <span class="comment">// 如果设备已经绑定了驱动程序，则返回 1</span></span><br><span class="line">			ret = <span class="number">1</span>;</span><br><span class="line">			<span class="keyword">goto</span> out_unlock;</span><br><span class="line">		&#125;</span><br><span class="line">        <span class="comment">// 尝试将设备与驱动程序进行绑定</span></span><br><span class="line">		ret = device_bind_driver(dev);</span><br><span class="line">		<span class="keyword">if</span> (ret == <span class="number">0</span>)</span><br><span class="line">			ret = <span class="number">1</span>;</span><br><span class="line">		<span class="keyword">else</span> &#123;</span><br><span class="line">            <span class="comment">// 绑定失败，将设备的驱动程序指针置为 NULL</span></span><br><span class="line">			dev-&gt;driver = <span class="literal">NULL</span>;</span><br><span class="line">			ret = <span class="number">0</span>;</span><br><span class="line">		&#125;</span><br><span class="line">	&#125; <span class="keyword">else</span> &#123;</span><br><span class="line">        <span class="comment">// 如果设备没有驱动程序，需要遍历总线上的驱动程序进行匹配</span></span><br><span class="line">		<span class="keyword">struct</span> device_attach_data data = &#123;</span><br><span class="line">			.dev = dev,</span><br><span class="line">			.check_async = allow_async,</span><br><span class="line">			.want_async = <span class="literal">false</span>,</span><br><span class="line">		&#125;;</span><br><span class="line">		<span class="comment">// 如果设备有父设备，调用 pm_runtime_get_sync() 增加父设备的引用计数</span></span><br><span class="line">		<span class="keyword">if</span> (dev-&gt;parent)</span><br><span class="line">			pm_runtime_get_sync(dev-&gt;parent);</span><br><span class="line">		<span class="comment">// 遍历总线上的驱动程序，调用 __device_attach_driver() 进行匹配</span></span><br><span class="line">		ret = bus_for_each_drv(dev-&gt;bus, <span class="literal">NULL</span>, &amp;data,</span><br><span class="line">					__device_attach_driver);</span><br><span class="line">		<span class="keyword">if</span> (!ret &amp;&amp; allow_async &amp;&amp; data.have_async) &#123;</span><br><span class="line">			<span class="comment">/*</span></span><br><span class="line"><span class="comment">			 * If we could not find appropriate driver</span></span><br><span class="line"><span class="comment">			 * synchronously and we are allowed to do</span></span><br><span class="line"><span class="comment">			 * async probes and there are drivers that</span></span><br><span class="line"><span class="comment">			 * want to probe asynchronously, we&#x27;ll</span></span><br><span class="line"><span class="comment">			 * try them.</span></span><br><span class="line"><span class="comment">			 */</span></span><br><span class="line">            <span class="comment">/*</span></span><br><span class="line"><span class="comment">             * 如果无法同步找到适合的驱动程序，并且允许异步探测以及有驱动程序要求异步探测，</span></span><br><span class="line"><span class="comment">             * 则尝试进行异步探测。</span></span><br><span class="line"><span class="comment">             */</span></span><br><span class="line">			dev_dbg(dev, <span class="string">&quot;scheduling asynchronous probe\n&quot;</span>);</span><br><span class="line">            <span class="comment">// 增加设备的引用计数，以确保在异步探测期间设备不会被释放</span></span><br><span class="line">			get_device(dev);</span><br><span class="line">            <span class="comment">// 调度异步任务 __device_attach_async_helper() 进行异步探测</span></span><br><span class="line">			async_schedule(__device_attach_async_helper, dev);</span><br><span class="line">		&#125; <span class="keyword">else</span> &#123;</span><br><span class="line">            <span class="comment">// 如果无法异步探测或者没有驱动程序要求异步探测，则调用 pm_request_idle() 进入空闲状态</span></span><br><span class="line">			pm_request_idle(dev);</span><br><span class="line">		&#125;</span><br><span class="line">		<span class="comment">// 如果设备有父设备，调用 pm_runtime_put() 减少父设备的引用计数</span></span><br><span class="line">		<span class="keyword">if</span> (dev-&gt;parent)</span><br><span class="line">			pm_runtime_put(dev-&gt;parent);</span><br><span class="line">	&#125;</span><br><span class="line">out_unlock:</span><br><span class="line">	device_unlock(dev); <span class="comment">// 解锁设备</span></span><br><span class="line">	<span class="keyword">return</span> ret;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<ul>
<li>第 <a target="_blank" rel="noopener" href="https://elixir.bootlin.com/linux/v4.19.71/source/drivers/base/dd.c#L791">791</a> 行</li>
</ul>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">device_lock(dev);</span><br></pre></td></tr></table></figure>

<p>通过调用 <a target="_blank" rel="noopener" href="https://elixir.bootlin.com/linux/v4.19.71/source/include/linux/device.h#L1164">device_lock(dev)</a> 锁定设备，确保在进行设备附加操作时不会被其他线程干扰。</p>
<ul>
<li>第 <a target="_blank" rel="noopener" href="https://elixir.bootlin.com/linux/v4.19.71/source/drivers/base/dd.c#L792">792</a> - <a target="_blank" rel="noopener" href="https://elixir.bootlin.com/linux/v4.19.71/source/drivers/base/dd.c#L804">804</a> 行</li>
</ul>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">if</span> (dev-&gt;driver) &#123;</span><br><span class="line">	<span class="keyword">if</span> (device_is_bound(dev)) &#123;</span><br><span class="line">		ret = <span class="number">1</span>;</span><br><span class="line">		<span class="keyword">goto</span> out_unlock;</span><br><span class="line">	&#125;</span><br><span class="line">	ret = device_bind_driver(dev);</span><br><span class="line">	<span class="keyword">if</span> (ret == <span class="number">0</span>)</span><br><span class="line">		ret = <span class="number">1</span>;</span><br><span class="line">	<span class="keyword">else</span> &#123;</span><br><span class="line">		dev-&gt;driver = <span class="literal">NULL</span>;</span><br><span class="line">		ret = <span class="number">0</span>;</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>检查 <a target="_blank" rel="noopener" href="https://elixir.bootlin.com/linux/v4.19.71/source/include/linux/device.h#L900">dev-&gt;driver</a> 成员是否为空，此处非空的情况。设备已经绑定了驱动程序，则返回 1。如果设备没有绑定驱动程序，则尝试将设备与驱动程序进行绑定。如果绑定成功，返回 1；否则，将设备的驱动程序指针置为 NULL，并返回 0。</p>
<ul>
<li>第 <a target="_blank" rel="noopener" href="https://elixir.bootlin.com/linux/v4.19.71/source/drivers/base/dd.c#L805">805</a> - <a target="_blank" rel="noopener" href="https://elixir.bootlin.com/linux/v4.19.71/source/drivers/base/dd.c#L809">809</a> 行</li>
</ul>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">struct</span> <span class="title">device_attach_data</span> <span class="title">data</span> =</span> &#123;</span><br><span class="line">	.dev = dev,</span><br><span class="line">	.check_async = allow_async,</span><br><span class="line">	.want_async = <span class="literal">false</span>,</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>

<p><a target="_blank" rel="noopener" href="https://elixir.bootlin.com/linux/v4.19.71/source/include/linux/device.h#L900">dev-&gt;driver</a> 为空时，那么需要遍历总线上的驱动程序进行匹配。为此，定义了一个结构体 <a target="_blank" rel="noopener" href="https://elixir.bootlin.com/linux/v4.19.71/source/drivers/base/dd.c#L688">struct device_attach_data</a>，其中包含了设备、是否允许异步 探测以及是否有驱动程序要求异步探测的信息。 </p>
<ul>
<li>第 <a target="_blank" rel="noopener" href="https://elixir.bootlin.com/linux/v4.19.71/source/drivers/base/dd.c#L811">811</a> - <a target="_blank" rel="noopener" href="https://elixir.bootlin.com/linux/v4.19.71/source/drivers/base/dd.c#L812">812</a> 行</li>
</ul>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">if</span> (dev-&gt;parent)</span><br><span class="line">	pm_runtime_get_sync(dev-&gt;parent);</span><br></pre></td></tr></table></figure>

<p>如果设备有父设备，调用  <a target="_blank" rel="noopener" href="https://elixir.bootlin.com/linux/v4.19.71/source/include/linux/pm_runtime.h#L223">pm_runtime_get_sync(dev-&gt;parent)</a> 增加父设备的引用计数。 </p>
<p>第 <a target="_blank" rel="noopener" href="https://elixir.bootlin.com/linux/v4.19.71/source/drivers/base/dd.c#L814">814</a> - <a target="_blank" rel="noopener" href="https://elixir.bootlin.com/linux/v4.19.71/source/drivers/base/dd.c#L829">829</a> 行</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line">ret = bus_for_each_drv(dev-&gt;bus, <span class="literal">NULL</span>, &amp;data,</span><br><span class="line">			__device_attach_driver);</span><br><span class="line"><span class="keyword">if</span> (!ret &amp;&amp; allow_async &amp;&amp; data.have_async) &#123;</span><br><span class="line">	<span class="comment">/*</span></span><br><span class="line"><span class="comment">	 * If we could not find appropriate driver</span></span><br><span class="line"><span class="comment">	 * synchronously and we are allowed to do</span></span><br><span class="line"><span class="comment">	 * async probes and there are drivers that</span></span><br><span class="line"><span class="comment">	 * want to probe asynchronously, we&#x27;ll</span></span><br><span class="line"><span class="comment">	 * try them.</span></span><br><span class="line"><span class="comment">	 */</span></span><br><span class="line">	dev_dbg(dev, <span class="string">&quot;scheduling asynchronous probe\n&quot;</span>);</span><br><span class="line">	get_device(dev);</span><br><span class="line">	async_schedule(__device_attach_async_helper, dev);</span><br><span class="line">&#125; <span class="keyword">else</span> &#123;</span><br><span class="line">	pm_request_idle(dev);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>调用  <a target="_blank" rel="noopener" href="https://elixir.bootlin.com/linux/v4.19.71/source/drivers/base/bus.c#L432">bus_for_each_drv(dev-&gt;bus, NULL, &amp;data, __device_attach_driver)</a> 遍历总线上的驱动 程序，并调用  <a target="_blank" rel="noopener" href="https://elixir.bootlin.com/linux/v4.19.71/source/drivers/base/dd.c#L721">__device_attach_driver()</a>进行匹配。<a target="_blank" rel="noopener" href="https://elixir.bootlin.com/linux/v4.19.71/source/drivers/base/dd.c#L721">__device_attach_driver()</a> 是一个回调函数，用于判断驱动程序是否适配当前设备。 </p>
<p>如果无法同步找到适合的驱动程序，并且允许异步探测以及有驱动程序要求异步探测，则调度异步任务 <a target="_blank" rel="noopener" href="https://elixir.bootlin.com/linux/v4.19.71/source/drivers/base/dd.c#L751">__device_attach_async_helper()</a> 进行异步探测。在异步探测之前，会增加设备的引用计数以确保设备在异步探测期间不会被释放。异步探测会在后台进行，不会阻塞当前线程。</p>
<p>如果无法异步探测或者没有驱动程序要求异步探测，则调用 <a target="_blank" rel="noopener" href="https://elixir.bootlin.com/linux/v4.19.71/source/include/linux/pm_runtime.h#L203">pm_request_idle(dev)</a> 进入空 闲状态，让设备进入省电模式。</p>
<h2 id="3-device-bind-driver"><a href="#3-device-bind-driver" class="headerlink" title="3. device_bind_driver()"></a><font size=3>3. <a target="_blank" rel="noopener" href="https://elixir.bootlin.com/linux/v4.19.71/source/drivers/base/dd.c#L406">device_bind_driver()</a></font></h2><p><a target="_blank" rel="noopener" href="https://elixir.bootlin.com/linux/v4.19.71/source/drivers/base/dd.c#L406">device_bind_driver()</a> 函数定义如下：</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="type">int</span> <span class="title function_">device_bind_driver</span><span class="params">(<span class="keyword">struct</span> device *dev)</span></span><br><span class="line">&#123;</span><br><span class="line">	<span class="type">int</span> ret;</span><br><span class="line"></span><br><span class="line">	ret = driver_sysfs_add(dev);</span><br><span class="line">	<span class="keyword">if</span> (!ret)</span><br><span class="line">		driver_bound(dev);</span><br><span class="line">	<span class="keyword">else</span> <span class="keyword">if</span> (dev-&gt;bus)</span><br><span class="line">		blocking_notifier_call_chain(&amp;dev-&gt;bus-&gt;p-&gt;bus_notifier,</span><br><span class="line">					     BUS_NOTIFY_DRIVER_NOT_BOUND, dev);</span><br><span class="line">	<span class="keyword">return</span> ret;</span><br><span class="line">&#125;</span><br><span class="line">EXPORT_SYMBOL_GPL(device_bind_driver);</span><br></pre></td></tr></table></figure>

<p>函数中调用 <a target="_blank" rel="noopener" href="https://elixir.bootlin.com/linux/v4.19.71/source/drivers/base/dd.c#L321">driver_bound()</a> 函数完成设备和驱动程序的绑定，这个函数定义如下：</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br></pre></td><td class="code"><pre><span class="line"><span class="type">static</span> <span class="type">void</span> <span class="title function_">driver_bound</span><span class="params">(<span class="keyword">struct</span> device *dev)</span></span><br><span class="line">&#123;</span><br><span class="line">    <span class="comment">// 如果设备已经绑定了驱动程序，则输出警告信息并返回</span></span><br><span class="line">	<span class="keyword">if</span> (device_is_bound(dev)) &#123;</span><br><span class="line">		printk(KERN_WARNING <span class="string">&quot;%s: device %s already bound\n&quot;</span>,</span><br><span class="line">			__func__, kobject_name(&amp;dev-&gt;kobj));</span><br><span class="line">		<span class="keyword">return</span>;</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="comment">// 输出绑定信息</span></span><br><span class="line">	pr_debug(<span class="string">&quot;driver: &#x27;%s&#x27;: %s: bound to device &#x27;%s&#x27;\n&quot;</span>, dev-&gt;driver-&gt;name,</span><br><span class="line">		 __func__, dev_name(dev));</span><br><span class="line">	<span class="comment">// 将设备添加到驱动程序的设备链表中</span></span><br><span class="line">	klist_add_tail(&amp;dev-&gt;p-&gt;knode_driver, &amp;dev-&gt;driver-&gt;p-&gt;klist_devices);</span><br><span class="line">    <span class="comment">// 更新设备的驱动程序链接状态</span></span><br><span class="line">	device_links_driver_bound(dev);</span><br><span class="line">	<span class="comment">// 检查设备的电源管理回调函数</span></span><br><span class="line">	device_pm_check_callbacks(dev);</span><br><span class="line"></span><br><span class="line">	<span class="comment">/*</span></span><br><span class="line"><span class="comment">	 * Make sure the device is no longer in one of the deferred lists and</span></span><br><span class="line"><span class="comment">	 * kick off retrying all pending devices</span></span><br><span class="line"><span class="comment">	 */</span></span><br><span class="line">    <span class="comment">/*</span></span><br><span class="line"><span class="comment">     * 确保设备不再位于延迟探测列表中，并启动重试所有待处理设备</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">	driver_deferred_probe_del(dev);</span><br><span class="line">	driver_deferred_probe_trigger();</span><br><span class="line">	<span class="comment">// 如果设备有总线，调用总线通知链进行通知</span></span><br><span class="line">	<span class="keyword">if</span> (dev-&gt;bus)</span><br><span class="line">		blocking_notifier_call_chain(&amp;dev-&gt;bus-&gt;p-&gt;bus_notifier,</span><br><span class="line">					     BUS_NOTIFY_BOUND_DRIVER, dev);</span><br><span class="line">	<span class="comment">// 发送内核对象事件通知</span></span><br><span class="line">	kobject_uevent(&amp;dev-&gt;kobj, KOBJ_BIND);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p><a target="_blank" rel="noopener" href="https://elixir.bootlin.com/linux/v4.19.71/source/drivers/base/dd.c#L321">driver_bound()</a> 函数的作用是将驱动和设备进行绑定，首先，通过调用 <a target="_blank" rel="noopener" href="https://elixir.bootlin.com/linux/v4.19.71/source/drivers/base/dd.c#L307">device_is_bound(dev)</a> 检查设备是否已经绑定了驱动程序。如果设备已经绑定了驱动程序，则输出警告信息并返回。如果设备未绑定驱动程序，将输出绑定信息，其中包括驱动程序的名称、函数名和设备的名称。接下 来，通过调用 <a target="_blank" rel="noopener" href="https://elixir.bootlin.com/linux/v4.19.71/source/lib/klist.c#L130">klist_add_tail()</a>将设备添加到驱动程序的设备链表中。这样，驱动程序可以通 过遍历该链表来访问所有已绑定的设备。 然后，调用 <a target="_blank" rel="noopener" href="https://elixir.bootlin.com/linux/v4.19.71/source/drivers/base/core.c#L455">device_links_driver_bound()</a>更新设备的驱动程序链接状态。这个函数会确保设备和驱动程序之间的链接关系是正确的。</p>
<h2 id="4-总结"><a href="#4-总结" class="headerlink" title="4. 总结"></a><font size=3>4. 总结</font></h2><p>先加载驱动，后加载设备的时候，设备和驱动匹配的时候的函数调用关系如下：</p>
<img data-src="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/02IMX6ULL%E5%B9%B3%E5%8F%B0/LV06-%E9%A9%B1%E5%8A%A8%E5%BC%80%E5%8F%91/LV06-04-linux%E8%AE%BE%E5%A4%87%E6%A8%A1%E5%9E%8B-09-%E8%AE%BE%E5%A4%87%E4%B8%8E%E9%A9%B1%E5%8A%A8%E7%9A%84%E5%8C%B9%E9%85%8D/img/image-20250115142202576.png" alt="image-20250115142202576"  />

<h1 id="四、总结"><a href="#四、总结" class="headerlink" title="四、总结"></a><font size=3>四、总结</font></h1><h2 id="1-设备和驱动数据结构关系"><a href="#1-设备和驱动数据结构关系" class="headerlink" title="1. 设备和驱动数据结构关系"></a><font size=3>1. 设备和驱动数据结构关系</font></h2><p>到为止简单地了解了总线、设备、驱动的数据结构以及注册&#x2F;注销接口函数。下图是总线关联上设备与驱动之后的数据结构关系图：</p>
<img data-src="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/02IMX6ULL%E5%B9%B3%E5%8F%B0/LV06-%E9%A9%B1%E5%8A%A8%E5%BC%80%E5%8F%91/LV06-04-linux%E8%AE%BE%E5%A4%87%E6%A8%A1%E5%9E%8B-09-%E8%AE%BE%E5%A4%87%E4%B8%8E%E9%A9%B1%E5%8A%A8%E7%9A%84%E5%8C%B9%E9%85%8D/img/linux_device_modle000.png" alt="/sys/bus目录" />

<h2 id="2-注册流程"><a href="#2-注册流程" class="headerlink" title="2. 注册流程"></a><font size=3>2. 注册流程</font></h2><p>设备和驱动的注册流程如下：</p>
<img data-src="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/02IMX6ULL%E5%B9%B3%E5%8F%B0/LV06-%E9%A9%B1%E5%8A%A8%E5%BC%80%E5%8F%91/LV06-04-linux%E8%AE%BE%E5%A4%87%E6%A8%A1%E5%9E%8B-09-%E8%AE%BE%E5%A4%87%E4%B8%8E%E9%A9%B1%E5%8A%A8%E7%9A%84%E5%8C%B9%E9%85%8D/img/linux_device_modle003.png" alt="/sys/bus目录" />

<p>系统启动之后会调用buses_init函数创建&#x2F;sys&#x2F;bus文件目录，这部分系统在开机时已经帮我们准备好了， 接下去就是通过总线注册函数bus_register进行总线注册，注册完总线后在总线的目录下生成devices文件夹和drivers文件夹， 最后分别通过device_register以及driver_register函数注册相对应的设备和驱动。</p>

    </div>

    
    
    

    <footer class="post-footer">




    <div>
        
            <div style="text-align:center;color: #ccc;font-size:14px;">
            ----------本文结束
            <i class="fas fa-fan fa-spin" style="color: #FF1493; font-size: 1rem"></i>
            感谢您的阅读----------
            </div>
        
    </div>





  
  <div class="my_post_copyright"> 
    <p><span>文章标题:</span><a href="/post/6e6a5ac3.html">LV06-04-linux设备模型-09-设备与驱动的匹配</a></p>
    <p><span>文章作者:</span><a href="/" title="欢迎访问 《苏木》 的学习笔记">苏木</a></p>
    <p><span>发布时间:</span>2025年01月16日 - 22:26</p>
    <p><span>最后更新:</span>2025年06月14日 - 00:25</p>
    <p><span>原始链接:</span><a href="/post/6e6a5ac3.html" title="LV06-04-linux设备模型-09-设备与驱动的匹配">https://sumumm.github.io/post/6e6a5ac3.html</a></p>
    <p><span>许可协议:</span><i class="fab fa-creative-commons"></i> <a rel="license" href= "https://creativecommons.org/licenses/by-nc-nd/4.0/" target="_blank" title="Attribution-NonCommercial-NoDerivatives 4.0 International (CC BY-NC-ND 4.0)">署名-非商业性使用-禁止演绎 4.0 国际</a> 转载请保留原文链接及作者。</p>  
  </div>
  


          <div class="post-tags">
              <a href="/tags/LV06-%E9%A9%B1%E5%8A%A8%E5%BC%80%E5%8F%91/" rel="tag"><i class="fa fa-tag"></i> LV06-驱动开发</a>
          </div>

        

          <div class="post-nav">
            <div class="post-nav-item">
                <a href="/post/85bb1a56.html" rel="prev" title="LV06-05-linux平台总线模型-01-平台总线简介">
                  <i class="fa fa-angle-left"></i> LV06-05-linux平台总线模型-01-平台总线简介
                </a>
            </div>
            <div class="post-nav-item">
                <a href="/post/eb412581.html" rel="next" title="LV06-04-linux设备模型-08-注册驱动到总线">
                  LV06-04-linux设备模型-08-注册驱动到总线 <i class="fa fa-angle-right"></i>
                </a>
            </div>
          </div>
    </footer>
  </article>
</div>






</div>
  </main>

  <footer class="footer">
    <div class="footer-inner">

  <div class="copyright">
    &copy; 2017 – 
    <span itemprop="copyrightYear">2025</span>
    <span class="with-love">
      <i class="fa fa-heart"></i>
    </span>
    <span class="author" itemprop="copyrightHolder">苏木</span>
  </div>
<div class="wordcount">
  <span class="post-meta-item">
    <span class="post-meta-item-icon">
      <i class="fa fa-chart-line"></i>
    </span>
      <span>站点总字数：</span>
    <span title="站点总字数">3.7m</span>
  </span>
  <span class="post-meta-item">
    <span class="post-meta-item-icon">
      <i class="fa fa-coffee"></i>
    </span>
      <span>站点阅读时长 &asymp;</span>
    <span title="站点阅读时长">225:26</span>
  </span>
</div>




    <span id="sitetime"></span>
    <script defer language=javascript>
        function siteTime()
        {
            window.setTimeout("siteTime()", 1000);
            var seconds = 1000;
            var minutes = seconds * 60;
            var hours = minutes * 60;
            var days = hours * 24;
            var years = days * 365;
            var today = new Date();
            var todayYear = today.getFullYear();
            var todayMonth = today.getMonth()+1;
            var todayDate = today.getDate();
            var todayHour = today.getHours();
            var todayMinute = today.getMinutes();
            var todaySecond = today.getSeconds();
            /*==================================================
            Date.UTC() -- 返回date对象距世界标准时间(UTC)1970年1月1日午夜之间的毫秒数(时间戳)
            year        - 作为date对象的年份，为4位年份值
            month       - 0-11之间的整数，做为date对象的月份
            day         - 1-31之间的整数，做为date对象的天数
            hours       - 0(午夜24点)-23之间的整数，做为date对象的小时数
            minutes     - 0-59之间的整数，做为date对象的分钟数
            seconds     - 0-59之间的整数，做为date对象的秒数
            microseconds - 0-999之间的整数，做为date对象的毫秒数
            ==================================================*/
            var t1 = Date.UTC(2017, 
                              5, 
                              19, 
                              0, 
                              0, 
                              0); //北京时间
            var t2 = Date.UTC(todayYear,todayMonth,todayDate,todayHour,todayMinute,todaySecond);
            var diff = t2-t1;
            var diffYears = Math.floor(diff/years);
            var diffDays = Math.floor((diff/days)-diffYears*365);
            var diffHours = Math.floor((diff-(diffYears*365+diffDays)*days)/hours);
            var diffMinutes = Math.floor((diff-(diffYears*365+diffDays)*days-diffHours*hours)/minutes);
            var diffSeconds = Math.floor((diff-(diffYears*365+diffDays)*days-diffHours*hours-diffMinutes*minutes)/seconds);
            document.getElementById("sitetime").innerHTML="已在这里 "+diffYears+" 年 "+diffDays+" 天 "+diffHours+" 小时 "+diffMinutes+" 分钟 "+diffSeconds+" 秒";
        }
        siteTime();
    </script>



    </div>
  </footer>

  
  <div class="back-to-top" role="button" aria-label="返回顶部">
    <i class="fa fa-arrow-up fa-lg"></i>
    <span>0%</span>
  </div>
  <div class="reading-progress-bar"></div>

<noscript>
  <div class="noscript-warning">Theme NexT works best with JavaScript enabled</div>
</noscript>


  
  <script src="https://cdnjs.cloudflare.com/ajax/libs/animejs/3.2.1/anime.min.js" integrity="sha256-XL2inqUJaslATFnHdJOi9GfQ60on8Wx1C2H8DYiN1xY=" crossorigin="anonymous"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/next-theme-pjax/0.6.0/pjax.min.js" integrity="sha256-vxLn1tSKWD4dqbMRyv940UYw4sXgMtYcK6reefzZrao=" crossorigin="anonymous"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/fancyapps-ui/5.0.28/fancybox/fancybox.umd.js" integrity="sha256-ytMJGN3toR+a84u7g7NuHm91VIR06Q41kMWDr2pq7Zo=" crossorigin="anonymous"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/lozad.js/1.16.0/lozad.min.js" integrity="sha256-mOFREFhqmHeQbXpK2lp4nA3qooVgACfh88fpJftLBbc=" crossorigin="anonymous"></script>
<script src="/js/comments.js"></script><script src="/js/utils.js"></script><script src="/js/motion.js"></script><script src="/js/next-boot.js"></script><script src="/js/pjax.js"></script>

  <script src="https://cdnjs.cloudflare.com/ajax/libs/hexo-generator-searchdb/1.4.1/search.js" integrity="sha256-1kfA5uHPf65M5cphT2dvymhkuyHPQp5A53EGZOnOLmc=" crossorigin="anonymous"></script>
<script src="/js/third-party/search/local-search.js"></script>




  <script src="/js/third-party/fancybox.js"></script>

  <script src="/js/third-party/pace.js"></script>


  




  

  <script class="next-config" data-name="enableMath" type="application/json">true</script><script class="next-config" data-name="mathjax" type="application/json">{"enable":true,"tags":"none","js":{"url":"https://cdnjs.cloudflare.com/ajax/libs/mathjax/3.2.2/es5/tex-mml-chtml.js","integrity":"sha256-MASABpB4tYktI2Oitl4t+78w/lyA+D7b/s9GEP0JOGI="}}</script>
<script src="/js/third-party/math/mathjax.js"></script>


 
        <div id="click-show-text"
            data-mobile = false
            data-text = 富强,民主,文明,和谐,自由,平等,公正,法制,爱国,敬业,诚信,友善
            data-fontsize = 15px
            data-random= false>
        </div>
       

      
        <script async src=https://cdn.jsdelivr.net/npm/hexo-next-mouse-effect@latest/click/showText.js></script>
      

      
    




    <script async src="/js/fancybox_param.js"></script>





<!-- APlayer本体 -->



</body>
</html>
